Module: Mince::Model::Fields::ClassMethods
- Defined in:
- lib/mince/model/fields.rb
Instance Method Summary collapse
-
#add_assignable_field(field_name) ⇒ Object
Adds an assignable field, values for these fields can be set using the field writer or from the
attributes=method. -
#add_readonly_field(field_name) ⇒ Object
Adds a read only field, values for these fields can only be set in the class itself or from the hash sent in to the initializer.
-
#assignable_fields ⇒ Object
Returns the list of assignable fields.
-
#field(field_name, options = {}) ⇒ Object
Adds a field to the object.
-
#fields(*field_names) ⇒ Object
Adds multiple readonly fields to the fields array, and returns the current list of fields.
-
#readonly_fields ⇒ Object
Returns the list of readonly fields.
Instance Method Details
#add_assignable_field(field_name) ⇒ Object
Adds an assignable field, values for these fields can be set using the field writer
or from the attributes= method.
41 42 43 44 45 |
# File 'lib/mince/model/fields.rb', line 41 def add_assignable_field(field_name) fields << field_name assignable_fields << field_name attr_accessor field_name end |
#add_readonly_field(field_name) ⇒ Object
Adds a read only field, values for these fields can only be set in the class itself or from the hash sent in to the initializer
33 34 35 36 37 |
# File 'lib/mince/model/fields.rb', line 33 def add_readonly_field(field_name) fields << field_name readonly_fields << field_name attr_reader field_name end |
#assignable_fields ⇒ Object
Returns the list of assignable fields
56 57 58 |
# File 'lib/mince/model/fields.rb', line 56 def assignable_fields @assignable_fields ||= [] end |
#field(field_name, options = {}) ⇒ Object
Adds a field to the object. Takes options to indicate assignability. If assignable
is set, the field will be assignable via
model.field = 'foo'
model.attributes = { field: 'foo' }
23 24 25 26 27 28 29 |
# File 'lib/mince/model/fields.rb', line 23 def field(field_name, ={}) if [:assignable] add_assignable_field(field_name) else add_readonly_field(field_name) end end |
#fields(*field_names) ⇒ Object
Adds multiple readonly fields to the fields array, and returns the current list of fields. If no fields are given, it just returns the current list of fields
49 50 51 52 53 |
# File 'lib/mince/model/fields.rb', line 49 def fields(*field_names) @fields ||= [] field_names.each {|field_name| field(field_name) } @fields end |
#readonly_fields ⇒ Object
Returns the list of readonly fields
61 62 63 |
# File 'lib/mince/model/fields.rb', line 61 def readonly_fields @readonly_fields ||= [] end |