Class: Adva::Static::Import::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/adva/static/import/model/base.rb

Direct Known Subclasses

Post, Section, Site

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Base

Returns a new instance of Base.



10
11
12
# File 'lib/adva/static/import/model/base.rb', line 10

def initialize(source)
  @source = source.is_a?(Source::Base) ? source : Source.build(model_name, source)
end

Instance Attribute Details

#attribute_namesObject (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

#sourceObject (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

Returns:

  • (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

#attributesObject



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

#modelObject



40
41
42
# File 'lib/adva/static/import/model/base.rb', line 40

def model
  model_name.constantize
end

#model_nameObject



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_recordObject



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