Class: Justa::JustaObject

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

Direct Known Subclasses

Model

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = {}) ⇒ JustaObject

Returns a new instance of JustaObject.



9
10
11
12
13
14
15
16
17
# File 'lib/justa/object.rb', line 9

def initialize(response = {})
  # raise MissingCredentialsError.new("Missing :client_key for extra options #{options}") if options && !options[:client_key]

  @attributes = {}
  @unsaved_attributes = Set.new

  @client_key = response.dig(:client_key) || Justa.default_client_key # || :default
  update response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/justa/object.rb', line 96

def method_missing(name, *args, &block)
  name = 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

    return self[name] || self[name.to_sym] if args.size == 0
  end

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

  super name, *args, &block
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/justa/object.rb', line 3

def attributes
  @attributes
end

Class Method Details

.convert(response, resource_name = nil, client_key = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/justa/object.rb', line 114

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

Instance Method Details

#==(other) ⇒ Object



28
29
30
# File 'lib/justa/object.rb', line 28

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

#[]=(key, value) ⇒ Object



19
20
21
22
# File 'lib/justa/object.rb', line 19

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

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/justa/object.rb', line 24

def empty?
  @attributes.empty?
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/justa/object.rb', line 50

def respond_to?(name, include_all = false)
  return true if name.to_s.end_with? "="

  @attributes.has_key?(name.to_s) || super
end

#to_hashObject



38
39
40
41
42
# File 'lib/justa/object.rb', line 38

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

#to_request_paramsObject



44
45
46
47
48
# File 'lib/justa/object.rb', line 44

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

#unsaved_attributesObject



32
33
34
35
36
# File 'lib/justa/object.rb', line 32

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