Class: GrapeClient::Base

Inherits:
Object
  • Object
show all
Extended by:
Accessors, RestMethodsCollection
Includes:
RestMethodsMember
Defined in:
lib/grape_client/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Accessors

mattr_accessor

Methods included from RestMethodsCollection

all, create, find, where

Methods included from RestMethodsMember

#destroy, #reload, #save, #save!

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



60
61
62
# File 'lib/grape_client/base.rb', line 60

def initialize(attrs = {})
  self.attributes = attrs
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/grape_client/base.rb', line 85

def method_missing(m, *args)
  m = m.to_s
  if m.last == '='
    name = m[0..-2]
    self[name] = args.first
  else
    self[m]
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/grape_client/base.rb', line 9

def attributes
  @attributes
end

Class Method Details

.attributesObject



54
55
56
57
# File 'lib/grape_client/base.rb', line 54

def attributes
  class_variable_set('@@attributes', []) unless class_variable_defined?('@@attributes')
  class_variable_get('@@attributes')
end

.belongs_to(property, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/grape_client/base.rb', line 19

def belongs_to(property, options = {})
  field_accessor "#{property}_id"

  define_method(property) do
    @attributes[property] ||= retrive_object(options[:class_name] || property,
                                             self["#{property}_id"])
  end

  define_method("#{property}=") do |object|
    self["#{property}_id"] = object.id
    @attributes[property] = object
  end

  define_method("#{property}_id=") do |id|
    @attributes[property] = nil
    self["#{property}_id"] = id
  end
end

.cacheObject



42
43
44
# File 'lib/grape_client/base.rb', line 42

def cache
  Cache.instance
end

.connectionObject



38
39
40
# File 'lib/grape_client/base.rb', line 38

def connection
  @connection ||= Connection.new(user, password)
end

.endpointObject



50
51
52
# File 'lib/grape_client/base.rb', line 50

def endpoint
  site + prefix + entity_name.pluralize
end

.entity_nameObject



46
47
48
# File 'lib/grape_client/base.rb', line 46

def entity_name
  name.split('::').last.underscore
end

.field_accessor(*names) ⇒ Object



12
13
14
15
16
17
# File 'lib/grape_client/base.rb', line 12

def field_accessor(*names)
  attributes = self.attributes
  names.each do |name|
    attributes << name.to_sym
  end
end

Instance Method Details

#[](name) ⇒ Object

Raises:



73
74
75
76
77
# File 'lib/grape_client/base.rb', line 73

def [](name)
  name = name.to_sym
  raise NameError, name unless self.class.attributes.include? name
  @attributes[name]
end

#[]=(name, value) ⇒ Object

Raises:



79
80
81
82
83
# File 'lib/grape_client/base.rb', line 79

def []=(name, value)
  name = name.to_sym
  raise NameError, name unless self.class.attributes.include? name
  @attributes[name] = value
end

#to_postObject



95
96
97
98
99
100
# File 'lib/grape_client/base.rb', line 95

def to_post
  entity_name = self.class.entity_name
  list = self.class.attributes
  filtered_attributes = attributes.select { |key, _value| list.include? key }
  filtered_attributes.transform_keys { |key| "#{entity_name}[#{key}]" }
end