Project

masterman

0.0
No commit activity in last 3 years
No release in over 3 years
Masterman is static data loader for Ruby. It loads data from direct or file, and defines accessor to read attributes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
>= 2.13.0, ~> 2.13
>= 4.6.4, ~> 4.6
>= 0.9.12, ~> 0.9
>= 3.4.0, ~> 3.4

Runtime

 Project Readme

Masterman

Unstable version

Masterman is static data loader for Ruby. It loads data from direct or file, and defines accessor to read attributes.

Installation

Add this line to your application's Gemfile:

gem 'masterman'

And then execute:

$ bundle

Or install it yourself as:

$ gem install masterman

Usage

class Prefecture
  include Masterman

  masterman do 
    attr_reader :id, :name
    mount path: '../prefecture.yml'
    has_many :shipping_costs
  end
end

class ShippingCost
  include Masterman

  masterman do 
    attr_reader :id, :price, prefecture_id
    mount path: '../shipping_cost.yml'
    belongs_to :prefecture
  end
end

ShippingCost.first.prefecture.is_a?(Prefecture)
ShippingCost.first.attributes # => { 'id' => ..., 'price' => ..., 'prefecture_id' => ... }

Directly mount single data to class

class Administrator
  include Masterman

  masterman do 
    # Can not have association
    cattr_reader :email, :name
    class_mount path: '../prefecture.yml'
  end
end

Prefecture.email.present? # => true

Select filetype of static data

class Item
  include Masterman

  masterman do
    # You can use either loader
    # mount direct: [{ id: 1 }]
    # mount path: 'item.yml', loader: :yml
    # mount path: 'item.csv', loader: :csv
    # mount path: 'item.json', loader: :json
  end
end

ActiveRecord-like associations

class Item
  include Masterman

  masterman do
    mount direct: [{ id: 1 }]
    belongs_to :user
    has_one :main_attachment
    has_many :variations
    has_many :attachments, through: :variations

    # Filter records by scope which is evaluated by using `#instance_exec`.
    has_many :odd_variations, -> { id % 2 == 1 }, source: :variations
  end
end

Enable cache

When masterman loader called .find or .all, loader is not cached. If you want to cache static data, call .mount with cache: true

class Item
  include Masterman

  masterman do
    mount path: 'item.yml', cache: true
  end
end

TODO

  • Support has_and_belongs_to association
  • Should I support lazy loading?
  • Support cache records.
  • Validate options for association
  • Try to install masterman to pixivFACTORY

License

The gem is available as open source under the terms of the MIT License.