Class: Conexa::ConexaObject

Inherits:
Object show all
Defined in:
lib/conexa/object.rb

Overview

Base class for all Conexa objects with dynamic attribute access

Attribute Access via method_missing

Attributes can be accessed using either snake_case or camelCase:

customer.company_id  # => 3
customer.companyId   # => 3 (converted to snake_case internally)

The API returns camelCase attributes which are stored as snake_case. method_missing automatically converts any camelCase calls to snake_case.

Attribute Assignment

Attributes can be set using snake_case:

customer.name = "New Name"
customer.save

Direct Known Subclasses

Address, Auth, LegalPerson, Model, Pagination, Result

Constant Summary collapse

RESOURCES =
Dir[File.expand_path('../resources/*.rb', __FILE__)].map do |path|
  File.basename(path, '.rb').to_sym
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = {}) ⇒ ConexaObject

Returns a new instance of ConexaObject.



28
29
30
31
32
33
# File 'lib/conexa/object.rb', line 28

def initialize(response = {})
  @attributes = Hash.new
  @unsaved_attributes = Set.new

  update response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/conexa/object.rb', line 117

def method_missing(name, *args, &block)
  name = Util.to_snake_case(name.to_s)

  unless block_given?
    if name.end_with?('=') && args.size == 1
      attribute_name = name[0...-1]
      return self[attribute_name] = args[0]
    end

    if args.size == 0
      if @attributes.key?(name)
        return @attributes[name]
      elsif @attributes.key?(name.to_sym)
        return @attributes[name.to_sym]
      end
      return nil
    end
  end

  if attributes.respond_to? name
    return attributes.public_send name, *args, &block
  end

  super name, *args, &block
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



22
23
24
# File 'lib/conexa/object.rb', line 22

def attributes
  @attributes
end

Class Method Details

.convert(response, resource_name = nil) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/conexa/object.rb', line 144

def convert(response, resource_name = nil)
  case response
  when Array
    response.map{ |i| convert i, resource_name }
  when Hash
    resource_class_for(resource_name).new(response)
  else
    response
  end
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/conexa/object.rb', line 44

def ==(other)
  self.class == other.class && id == other.id
end

#[]=(key, value) ⇒ Object



35
36
37
38
# File 'lib/conexa/object.rb', line 35

def []=(key,value)
  @attributes[key] = value
  @unsaved_attributes.add key
end

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/conexa/object.rb', line 40

def empty?
  @attributes.empty?
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/conexa/object.rb', line 60

def respond_to_missing?(name, include_private = false)
  name_str = Util.to_snake_case(name.to_s)
  return true if name_str.end_with?('=')

  @attributes.key?(name_str) || @attributes.key?(name_str.to_sym) || super
end

#to_hashObject



54
55
56
57
58
# File 'lib/conexa/object.rb', line 54

def to_hash
  Hash[@attributes.map do |key, value|
    [ key, to_hash_value(value, :to_hash) ]
  end]
end

#unsaved_attributesObject



48
49
50
51
52
# File 'lib/conexa/object.rb', line 48

def unsaved_attributes
  Hash[@unsaved_attributes.map do |key|
    [ key, to_hash_value(self[key], :unsaved_attributes) ]
  end]
end