Class: Restify::Resource

Inherits:
Hashie::Hash
  • Object
show all
Includes:
Hashie::Extensions::IndifferentAccess, Hashie::Extensions::MethodReader, Contextual, Relations
Defined in:
lib/restify/resource.rb

Instance Method Summary collapse

Methods included from Relations

#rel, #rel?

Methods included from Contextual

#follow, #relations, #response

Constructor Details

#initialize(context, data = {}) ⇒ Resource

Returns a new instance of Resource.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/restify/resource.rb', line 10

def initialize(context, data = {})
  @context = context

  data.each_pair do |key, value|
    self[key.to_s] = convert_value(value)

    name = case key.to_s.downcase
      when /\A(\w+)_url\z/
        Regexp.last_match[1]
      when 'url'
        'self'
      else
        next
    end

    unless @context.relation?(name) || value.nil? || value.to_s.empty?
      @context.add_relation name, value.to_s
    end
  end
end

Instance Method Details

#==(other) ⇒ Object

Compare with other Restify::Resources or hashes.



33
34
35
36
37
38
39
40
41
42
# File 'lib/restify/resource.rb', line 33

def ==(other)
  case other
    when Resource
      super && relations == other.relations
    when Hash
      super Hash[other.map {|k, v| [convert_key(k), v] }]
    else
      super
  end
end

#convert_key(key) ⇒ Object



46
47
48
# File 'lib/restify/resource.rb', line 46

def convert_key(key)
  key.to_s
end

#convert_value(value) ⇒ Object



52
53
54
# File 'lib/restify/resource.rb', line 52

def convert_value(value)
  @context.inherit_value(value)
end