Class: Adva::Static::Import::Model::Base
- Inherits:
-
Object
- Object
- Adva::Static::Import::Model::Base
- Defined in:
- lib/adva/static/import/model/base.rb
Instance Attribute Summary collapse
-
#attribute_names ⇒ Object
readonly
Returns the value of attribute attribute_names.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #attribute?(name) ⇒ Boolean
- #attribute_value(name) ⇒ Object
- #attributes ⇒ Object
-
#initialize(source) ⇒ Base
constructor
A new instance of Base.
- #model ⇒ Object
- #model_name ⇒ Object
- #update! ⇒ Object
- #updated_record ⇒ Object
Constructor Details
Instance Attribute Details
#attribute_names ⇒ Object (readonly)
Returns the value of attribute attribute_names.
8 9 10 |
# File 'lib/adva/static/import/model/base.rb', line 8 def attribute_names @attribute_names end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
8 9 10 |
# File 'lib/adva/static/import/model/base.rb', line 8 def source @source end |
Instance Method Details
#attribute?(name) ⇒ Boolean
36 37 38 |
# File 'lib/adva/static/import/model/base.rb', line 36 def attribute?(name) source.data.key?(name) || respond_to?(name) end |
#attribute_value(name) ⇒ Object
32 33 34 |
# File 'lib/adva/static/import/model/base.rb', line 32 def attribute_value(name) respond_to?(name) ? self.send(name) : source.data.send(name) end |
#attributes ⇒ Object
22 23 24 25 26 |
# File 'lib/adva/static/import/model/base.rb', line 22 def attributes attributes = attribute_names.map { |name| [name, attribute_value(name)] if attribute_value(name) } # attribute?(name) attributes = Hashr.new(Hash[*attributes.compact.flatten_once]) record && record.id ? attributes.merge(:id => record.id.to_s) : attributes end |
#model ⇒ Object
40 41 42 |
# File 'lib/adva/static/import/model/base.rb', line 40 def model model_name.constantize end |
#model_name ⇒ Object
44 45 46 |
# File 'lib/adva/static/import/model/base.rb', line 44 def model_name @model_name ||= self.class.name.demodulize end |
#update! ⇒ Object
14 15 16 |
# File 'lib/adva/static/import/model/base.rb', line 14 def update! updated_record.save! end |
#updated_record ⇒ Object
18 19 20 |
# File 'lib/adva/static/import/model/base.rb', line 18 def updated_record record.tap { |record| record.attributes = attributes.to_hash.stringify_keys } end |