Class: Ciesta::FieldList
- Inherits:
-
Object
- Object
- Ciesta::FieldList
- Defined in:
- lib/ciesta/field_list.rb
Overview
List of object fields
Class Method Summary collapse
-
.define(definitions) ⇒ Object
private
Define a new field list.
Instance Method Summary collapse
-
#<<(field) ⇒ Object
private
Adds new field to list.
-
#[](name) ⇒ Ciesta::Field
private
Getting field by name.
-
#[]=(name, value) ⇒ Object
private
Setting field value.
-
#assign(attributes) ⇒ Boolean
Mass assign values to fields.
-
#assign!(attributes) ⇒ Boolean
Mass assign values to fields.
-
#attributes ⇒ Hash<Symbol, any>
private
Getting all field values.
-
#clear! ⇒ Object
private
Clear all fields.
-
#initialize ⇒ FieldList
constructor
Constructor.
-
#keys ⇒ Array<Symbol>
private
Getting all field names.
Constructor Details
#initialize ⇒ FieldList
Constructor
20 21 22 |
# File 'lib/ciesta/field_list.rb', line 20 def initialize @list = {} end |
Class Method Details
.define(definitions) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Define a new field list
13 14 15 16 17 |
# File 'lib/ciesta/field_list.rb', line 13 def self.define(definitions) definitions.each_with_object(new) do |(name, ), list| list << Ciesta::Field.new(name, ) end end |
Instance Method Details
#<<(field) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds new field to list
28 29 30 |
# File 'lib/ciesta/field_list.rb', line 28 def <<(field) list[field.name] = field end |
#[](name) ⇒ Ciesta::Field
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Getting field by name
38 39 40 |
# File 'lib/ciesta/field_list.rb', line 38 def [](name) list[name.to_sym].value end |
#[]=(name, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Setting field value
47 48 49 |
# File 'lib/ciesta/field_list.rb', line 47 def []=(name, value) list[name.to_sym].value = value end |
#assign(attributes) ⇒ Boolean
Mass assign values to fields
68 69 70 71 72 73 74 75 |
# File 'lib/ciesta/field_list.rb', line 68 def assign(attributes) attributes = attributes.keep_if { |key| keys.include?(key) } begin assign!(attributes) rescue Ciesta::FieldNotDefined # rubocop:disable Lint/HandleExceptions # do nothing end end |
#assign!(attributes) ⇒ Boolean
Mass assign values to fields
57 58 59 60 61 |
# File 'lib/ciesta/field_list.rb', line 57 def assign!(attributes) attributes.each { |name, value| self[name] = value } rescue NoMethodError => e raise Ciesta::FieldNotDefined, "Field #{e.name} is not specified" end |
#attributes ⇒ Hash<Symbol, any>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Getting all field values
89 90 91 |
# File 'lib/ciesta/field_list.rb', line 89 def attributes list.values.map { |field| [field.name, field.value] }.to_h end |
#clear! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clear all fields
96 97 98 |
# File 'lib/ciesta/field_list.rb', line 96 def clear! list.each_value(&:clear!) end |
#keys ⇒ Array<Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Getting all field names
81 82 83 |
# File 'lib/ciesta/field_list.rb', line 81 def keys list.keys end |