Class: ClioClient::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/clio_client/resource.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}, session = nil) ⇒ Resource

Returns a new instance of Resource.



28
29
30
31
32
33
# File 'lib/clio_client/resource.rb', line 28

def initialize(values = {}, session = nil)
  self.session = session
  values.each_pair do |k, v|
    self.send("#{k}=", v) if respond_to?("#{k}=") && !v.nil?
  end
end

Class Attribute Details

.attributesObject

Returns the value of attribute attributes.



46
47
48
# File 'lib/clio_client/resource.rb', line 46

def attributes
  @attributes
end

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



8
9
10
# File 'lib/clio_client/resource.rb', line 8

def session
  @session
end

Class Method Details

.has_association(name, klass, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/clio_client/resource.rb', line 66

def has_association(name, klass, options = {})
  attr_accessor "#{name}_id"
  attr_reader name
  self.attributes["#{name}_id".intern] = {type: :foreign_key}
  define_method "#{name}=" do |attributes|
    write_attribute("#{name}_id", attributes["id"])
    if options[:polymorphic]
      obj = polymorphic_object(attributes, options[:accepted_types])
      obj ||= klass.new(attributes, session)
    else
      obj = klass.new(attributes, session)
    end
    write_attribute("#{name}", obj)
  end      
end

.has_many_association(name, klass, options = {}) ⇒ Object



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

def has_many_association(name, klass, options = {})
  attr_reader name
  self.attributes[name.intern] = {type: :has_many_association}
  define_method "#{name}=" do |arr|
    many = arr.collect do |attributes| 
      if options[:polymorphic]
        obj = polymorphic_object(attributes, options[:accepted_types])
        obj ||= klass.new(attributes, session)
      else
        klass.new(attributes, session)
      end
    end
    write_attribute(name, many)
  end      
end

.inherited(subclass) ⇒ Object



60
61
62
63
64
# File 'lib/clio_client/resource.rb', line 60

def inherited(subclass)
  if !self.attributes.nil?
    subclass.attributes = self.attributes
  end
end

.inspectObject



10
11
12
13
14
15
# File 'lib/clio_client/resource.rb', line 10

def self.inspect
  attr_list = attributes.inject([]) do |a, (attr, opts)|
    a << "#{attr}: #{opts[:type]}"
  end * ", "
  "#{super}(#{attr_list})"
end

.set_attributes(attrs) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/clio_client/resource.rb', line 47

def set_attributes(attrs)
  self.attributes = attrs
  attrs.each_pair do |name, options|
    attr_reader name
    define_method "#{name}=" do |value|
      if options[:readonly] && !instance_variable_get("@#{name}").nil?
        raise AttributeReadOnly 
      end
      write_attribute("#{name}", convert_attribute(value, options))
    end
  end
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?



100
101
102
# File 'lib/clio_client/resource.rb', line 100

def ==(o)
  self.class == o.class && !self.id.nil? && self.id != "" && self.id == o.id
end

#[](val) ⇒ Object



105
106
107
# File 'lib/clio_client/resource.rb', line 105

def [](val)
  self.send(val)
end

#destroyObject

Raises:



126
127
128
129
# File 'lib/clio_client/resource.rb', line 126

def destroy
  raise ResourceNotSaved if self.id.nil?
  api.destroy(self.id)
end

#inspectObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/clio_client/resource.rb', line 17

def inspect
  attr_list = self.class.attributes.inject([]) do |a, (attr, opts)|
    if has_attribute?(attr)
      a << "#{attr}: #{self[attr].inspect}"
    else
      a
    end
  end * ", "      
  "#<#{self.class} #{attr_list}>"
end

#reloadObject

Raises:



121
122
123
124
# File 'lib/clio_client/resource.rb', line 121

def reload
  raise ResourceNotSaved if self.id.nil?      
  api.find(self.id)
end

#saveObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/clio_client/resource.rb', line 110

def save
  if self.id.nil?
    saved_item = api.create(self.to_params)
    self.id = saved_item && saved_item.id
    self
  else
    api.update(self.id, self.to_params)
    self
  end
end

#to_paramsObject



35
36
37
38
39
# File 'lib/clio_client/resource.rb', line 35

def to_params
  self.class.attributes.inject({}) do |h, (attr, opts)|
    has_attribute?(attr) ? h.merge(attr => paramify(self[attr])) : h
  end
end