Class: JsonApiModel::Model

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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.



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

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



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

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.



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

def client_class
  @client_class
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

Class Method Details

.connection(&block) ⇒ Object



19
20
21
# File 'lib/json_api_model/model.rb', line 19

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

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



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

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



13
14
15
16
17
# File 'lib/json_api_model/model.rb', line 13

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

.wraps(client_class) ⇒ Object



9
10
11
# File 'lib/json_api_model/model.rb', line 9

def wraps( client_class )
  @client_class = client_class
end

Instance Method Details

#as_jsonObject



56
57
58
# File 'lib/json_api_model/model.rb', line 56

def as_json
  client.as_json
end