Module: Spacer::Model::ClassMethods
- Defined in:
- lib/spacer/model.rb
Instance Method Summary collapse
-
#from_hash(hash) {|instance| ... } ⇒ Object
Instantiate a new instance of the class into which we are included and populate that instance’s attributes given the provided Hash.
-
#hash_settable_accessor(symbol, klass) ⇒ Object
- Declares an attribute named ::symbol
- which can be set with either an instance of ::klass
-
or a Hash which will be used to populate a new instance of ::klass::.
-
#hash_settable_list_accessor(symbol, klass) ⇒ Object
- Declares an attribute named ::symbol
- which can be set with either a list of instances of ::klass
-
or a list of Hashes which will be used to populate a new instance of ::klass::.
- #hash_settable_list_writer(symbol, klass) ⇒ Object
- #hash_settable_writer(symbol, klass) ⇒ Object
-
#populating_attr_accessor(*symbols) ⇒ Object
Create a standard attr_writer and a populating_attr_reader.
-
#populating_attr_reader(*symbols) ⇒ Object
Create a reader that will attempt to populate the model if it has not already been populated.
- #populating_hash_settable_accessor(symbol, klass) ⇒ Object
- #populating_hash_settable_list_accessor(symbol, klass) ⇒ Object
Instance Method Details
#from_hash(hash) {|instance| ... } ⇒ Object
Instantiate a new instance of the class into which we are included and populate that instance’s attributes given the provided Hash. Key names in the Hash should map to attribute names on the model.
37 38 39 40 41 |
# File 'lib/spacer/model.rb', line 37 def from_hash(hash) instance = new(hash) yield instance if block_given? instance end |
#hash_settable_accessor(symbol, klass) ⇒ Object
- Declares an attribute named ::symbol
- which can be set with either an instance of ::klass
or a Hash which will be used to populate a new instance of ::klass::.
74 75 76 77 |
# File 'lib/spacer/model.rb', line 74 def hash_settable_accessor(symbol, klass) attr_reader symbol hash_settable_writer(symbol, klass) end |
#hash_settable_list_accessor(symbol, klass) ⇒ Object
- Declares an attribute named ::symbol
- which can be set with either a list of instances of ::klass
or a list of Hashes which will be used to populate a new instance of ::klass::.
88 89 90 91 |
# File 'lib/spacer/model.rb', line 88 def hash_settable_list_accessor(symbol, klass) attr_reader symbol hash_settable_list_writer(symbol, klass) end |
#hash_settable_list_writer(symbol, klass) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/spacer/model.rb', line 93 def hash_settable_list_writer(symbol, klass) define_method("#{symbol}=") do |list| instance_variable_set("@#{symbol}", list.map do |item| item.kind_of?(Hash) ? klass.from_hash(item) : item end) end end |
#hash_settable_writer(symbol, klass) ⇒ Object
79 80 81 82 83 |
# File 'lib/spacer/model.rb', line 79 def hash_settable_writer(symbol, klass) define_method("#{symbol}=") do |value| instance_variable_set("@#{symbol}", value.kind_of?(Hash) ? klass.from_hash(value) : value) end end |
#populating_attr_accessor(*symbols) ⇒ Object
Create a standard attr_writer and a populating_attr_reader
45 46 47 48 |
# File 'lib/spacer/model.rb', line 45 def populating_attr_accessor(*symbols) attr_writer *symbols populating_attr_reader *symbols end |
#populating_attr_reader(*symbols) ⇒ Object
Create a reader that will attempt to populate the model if it has not already been populated
52 53 54 55 56 57 58 59 |
# File 'lib/spacer/model.rb', line 52 def populating_attr_reader(*symbols) symbols.each do |symbol| define_method(symbol) do populate unless populated? instance_variable_get("@#{symbol}") end end end |
#populating_hash_settable_accessor(symbol, klass) ⇒ Object
61 62 63 64 |
# File 'lib/spacer/model.rb', line 61 def populating_hash_settable_accessor(symbol, klass) populating_attr_reader symbol hash_settable_writer(symbol, klass) end |
#populating_hash_settable_list_accessor(symbol, klass) ⇒ Object
66 67 68 69 |
# File 'lib/spacer/model.rb', line 66 def populating_hash_settable_list_accessor(symbol, klass) populating_attr_reader symbol hash_settable_list_writer(symbol, klass) end |