Class: Infuser::Models::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers::Hashie
Defined in:
lib/infuser/models/base.rb

Direct Known Subclasses

Company, Contact, Invoice, InvoiceItem, OrderItem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Hashie

#camelize_hash, #safe_classify

Constructor Details

#initialize(data = {}) ⇒ Base

Returns a new instance of Base.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/infuser/models/base.rb', line 82

def initialize data = {}
  data.each do |key, value|
    send("#{key}=", value)
  end

  @collections = self.class.collection_names.each_with_object({}) do |name, hash|
    hash[name] = Infuser::Collections::Proxy.new(name)
  end

  @children = self.class.children_names.each_with_object({}) do |name, hash|
    hash[name] = Infuser::Collections::ChildProxy.new(self, name)
  end

  return self
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



80
81
82
# File 'lib/infuser/models/base.rb', line 80

def id
  @id
end

Class Method Details

.belongs_to(type) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/infuser/models/base.rb', line 45

def belongs_to type
  type = type.to_s.underscore

  define_method "#{type}=" do |arg|
    update_association(type, arg)
  end

  define_method "clear_#{type}" do
    clear_association(type)
  end

  define_method "#{type}" do
    fetch_association(type)
  end
end

.children_namesObject



16
17
18
# File 'lib/infuser/models/base.rb', line 16

def children_names
  @children_names || []
end

.collection_namesObject



12
13
14
# File 'lib/infuser/models/base.rb', line 12

def collection_names
  @collection_names || []
end

.define_schema(*cols) ⇒ Object



20
21
22
23
# File 'lib/infuser/models/base.rb', line 20

def define_schema *cols
  @schema = *cols
  attr_accessor *cols
end

.fieldsetObject



61
62
63
64
65
66
67
68
# File 'lib/infuser/models/base.rb', line 61

def fieldset
  core = schema.dup
  coll = collection_names.map { |name| Infuser.const_get(name.classify).schema.dup }
  (core + coll).flatten.compact.map do |f|
    # don't use titlecase or upcase or fields like CompanyID turn into company
    f.to_s.split('_').map { |w| w[0] = w[0].upcase; w }.join
  end
end

.has_collection(type) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/infuser/models/base.rb', line 25

def has_collection type
  type = type.to_s.underscore
  @collection_names ||= []
  @collection_names << type

  define_method type.to_s.underscore do
    collections[type]
  end
end

.has_many(type) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/infuser/models/base.rb', line 35

def has_many type
  type = type.to_s.underscore
  @children_names ||= []
  @children_names << type

  define_method type.to_s.underscore do
    children[type]
  end
end

.klass_nameObject



70
71
72
# File 'lib/infuser/models/base.rb', line 70

def klass_name
  name.split('::').last
end

.schemaObject



8
9
10
# File 'lib/infuser/models/base.rb', line 8

def schema
  @schema || []
end

.service_nameObject



74
75
76
# File 'lib/infuser/models/base.rb', line 74

def service_name
  "#{klass_name.underscore}_service".classify
end

Instance Method Details

#attributesObject



102
103
104
105
106
107
108
# File 'lib/infuser/models/base.rb', line 102

def attributes
  data = self.class.schema.dup.each_with_object({}) { |key, hash| hash[key] = send(key) }
  collections.each do |name, proxy|
    data[name] = proxy.map(&:attributes)
  end
  data
end

#dataObject



110
111
112
113
114
115
116
# File 'lib/infuser/models/base.rb', line 110

def data
  data = self.class.schema.dup.each_with_object({}) { |key, hash| hash[key] = send(key) }
  collections.each do |name, proxy|
    data.merge!(proxy.data)
  end
  camelize_hash(data).select { |k, v| !v.nil? }
end

#destroyObject



131
132
133
# File 'lib/infuser/models/base.rb', line 131

def destroy
  client.get("DataService.delete", klass_name, id)
end

#fieldsetObject



151
152
153
# File 'lib/infuser/models/base.rb', line 151

def fieldset
  self.class.fieldset
end

#inspectObject



98
99
100
# File 'lib/infuser/models/base.rb', line 98

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

#klass_nameObject



155
156
157
# File 'lib/infuser/models/base.rb', line 155

def klass_name
  self.class.klass_name
end

#loadObject



135
136
137
# File 'lib/infuser/models/base.rb', line 135

def load
  populate client.get("DataService.load", klass_name, id, fieldset)
end

#new_record?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/infuser/models/base.rb', line 118

def new_record?
  id.nil?
end

#persisted?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/infuser/models/base.rb', line 122

def persisted?
  !new_record?
end

#populate(hash) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/infuser/models/base.rb', line 139

def populate hash
  hash.each do |key, value|
    send("#{key.underscore}=", value) if respond_to?(key.underscore.to_sym)
  end

  collections.each do |name, proxy|
    proxy.parse(hash)
  end

  return self
end

#saveObject



126
127
128
129
# File 'lib/infuser/models/base.rb', line 126

def save
  new_record? ? add : update
  return self
end

#service_nameObject



159
160
161
# File 'lib/infuser/models/base.rb', line 159

def service_name
  self.class.service_name
end