Class: JsonApiModel::Model

Inherits:
Object
  • Object
show all
Includes:
Associatable
Defined in:
lib/json_api_model/model.rb

Constant Summary collapse

RESERVED_FIELDS =
[ :type, :id ]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



47
48
49
# File 'lib/json_api_model/model.rb', line 47

def initialize( attributes = {} )
  @client = self.class.client_class.new( attributes )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



51
52
53
54
55
# File 'lib/json_api_model/model.rb', line 51

def method_missing( m, *args, &block )
  client.send m, *args, &block
rescue NoMethodError
  raise NoMethodError, "No method `#{m}' found in #{self} or #{client}"
end

Class Attribute Details

.client_classObject (readonly)

Returns the value of attribute client_class.



6
7
8
# File 'lib/json_api_model/model.rb', line 6

def client_class
  @client_class
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



43
44
45
# File 'lib/json_api_model/model.rb', line 43

def client
  @client
end

Class Method Details

.connection(&block) ⇒ Object



17
18
19
# File 'lib/json_api_model/model.rb', line 17

def connection( &block )
  client_class.connection true, &block
end

.method_missing(m, *args, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/json_api_model/model.rb', line 21

def method_missing( m, *args, &block )
  result = client_class.send m, *args, &block
  case result
  when JsonApiClient::ResultSet
    JsonApiModel::ResultSet.new( result, self )
  when client_class
    new_from_client result
  else
    result
  end
rescue NoMethodError
  raise NoMethodError, "No method `#{m}' found in #{self} or #{client_class}"
end

.new_from_client(client) ⇒ Object



11
12
13
14
15
# File 'lib/json_api_model/model.rb', line 11

def new_from_client( client )
  model = new
  model.client = client
  model
end

.wraps(client_class) ⇒ Object



7
8
9
# File 'lib/json_api_model/model.rb', line 7

def wraps( client_class )
  @client_class = client_class
end