Class: Layer::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/layer/resource.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, client = self.class.client) ⇒ Resource

Returns a new instance of Resource.



26
27
28
29
# File 'lib/layer/resource.rb', line 26

def initialize(attributes = {}, client = self.class.client)
  self.attributes = attributes
  self.client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



47
48
49
50
51
52
53
54
55
56
# File 'lib/layer/resource.rb', line 47

def method_missing(method, *args, &block)
  if method.to_s =~ /=$/
    attribute = method.to_s.sub(/=$/, '')
    attributes[attribute] = args.first
  elsif attributes.has_key?(method.to_s)
    attributes[method.to_s]
  else
    super
  end
end

Class Attribute Details

.clientObject



19
20
21
# File 'lib/layer/resource.rb', line 19

def client
  @client ||= Client::Platform.new
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



24
25
26
# File 'lib/layer/resource.rb', line 24

def attributes
  @attributes
end

#clientObject

Returns the value of attribute client.



24
25
26
# File 'lib/layer/resource.rb', line 24

def client
  @client
end

Class Method Details

.class_nameObject



7
8
9
# File 'lib/layer/resource.rb', line 7

def class_name
  name.split('::')[-1]
end

.from_response(attributes, client) ⇒ Object



15
16
17
# File 'lib/layer/resource.rb', line 15

def from_response(attributes, client)
  new(attributes, client)
end

.urlObject



11
12
13
# File 'lib/layer/resource.rb', line 11

def url
  "/#{class_name.downcase.to_s}s"
end

Instance Method Details

#idObject



35
36
37
# File 'lib/layer/resource.rb', line 35

def id
  attributes['id']
end

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

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/layer/resource.rb', line 39

def respond_to_missing?(method, include_private = false)
  attribute = method.to_s.sub(/=$/, '')

  attributes.has_key?(attribute) || super
end

#urlObject



31
32
33
# File 'lib/layer/resource.rb', line 31

def url
  attributes['url'] || (id && "#{self.class.url}/#{Layer::Client.normalize_id(id)}")
end