Class: Conversocial::Resources::Models::Base

Inherits:
Object
  • Object
show all
Includes:
Utils::Strings
Defined in:
lib/conversocial/resources/models/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Strings

#demodulize

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
# File 'lib/conversocial/resources/models/base.rb', line 9

def initialize params={}
  disable_association_resolving do
    @loaded_attributes = {}
    @contents = []
    assign_attributes params
  end
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



7
8
9
# File 'lib/conversocial/resources/models/base.rb', line 7

def contents
  @contents
end

#loaded_attributesObject (readonly)

Returns the value of attribute loaded_attributes.



7
8
9
# File 'lib/conversocial/resources/models/base.rb', line 7

def loaded_attributes
  @loaded_attributes
end

Instance Method Details

#assign_attributes(params = {}) ⇒ Object



22
23
24
# File 'lib/conversocial/resources/models/base.rb', line 22

def assign_attributes params={}
  params.each { |k, v| send "#{k}=".to_sym, v }
end

#attributes(mark_not_yet_loaded = true) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/conversocial/resources/models/base.rb', line 30

def attributes mark_not_yet_loaded=true
  disable_association_resolving do
    self.class.fields.map do |field_name|
      val = send(field_name.to_sym)
      val = :not_yet_loaded if mark_not_yet_loaded && val.nil? && @loaded_attributes[field_name.to_sym].nil?
      [field_name, val]
    end.to_h
  end
end

#fieldsObject



26
27
28
# File 'lib/conversocial/resources/models/base.rb', line 26

def fields
  self.class.fields
end

#inspectObject



17
18
19
20
# File 'lib/conversocial/resources/models/base.rb', line 17

def inspect
  attr_list = attributes.map { |k,v| "#{k}: #{v}" } * ', '
  "#<#{self.class.name}(#{attr_list})>"
end

#refreshObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/conversocial/resources/models/base.rb', line 40

def refresh
  disable_association_resolving do
    fully_loaded_instance = query_engine.find id
    if fully_loaded_instance
      assign_attributes fully_loaded_instance.attributes(false)
    else
      fields.each do |f|
        @loaded_attributes[f.to_sym] = 1
      end
    end
  end
  self
end