Class: RestPki::RestPkiObject

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_pki/object.rb

Constant Summary collapse

RESOURCES =
Dir[File.expand_path('../resources/*.rb', __FILE__)].map do |path|
  File.basename(path, '.rb').to_sym
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = {}, object) ⇒ RestPkiObject

Returns a new instance of RestPkiObject.



9
10
11
12
13
# File 'lib/rest_pki/object.rb', line 9

def initialize(response = {}, object)
  @attributes         = Hash.new
  @unsaved_attributes = Set.new
  update(response, object)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rest_pki/object.rb', line 75

def method_missing(name, *args, &block)
  name = name.to_s

  unless block_given?
    if name.end_with?('=') && args.size == 1
      attribute_name = name[0...-1]
      return self[attribute_name] = args[0]
    end

    if args.size == 0
      return self[name]
    end
  end

  if attributes.respond_to? name
    return attributes.public_send name, *args, &block
  end

  super name, *args, &block
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/rest_pki/object.rb', line 3

def attributes
  @attributes
end

Class Method Details

.convert(response, object) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/rest_pki/object.rb', line 98

def convert(response, object)
  case response
  when Array
    response.map{ |i| convert(i, object) }
  when Hash
    resource_class_for(object).new(response, object)     #response['object']).new(response)
  else
    response
  end
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
# File 'lib/rest_pki/object.rb', line 30

def ==(other)
  self.class == other.class && id == other.id
end

#[]=(key, value) ⇒ Object



15
16
17
18
# File 'lib/rest_pki/object.rb', line 15

def []=(key,value)
  @attributes[key] = value
  @unsaved_attributes.add key
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rest_pki/object.rb', line 26

def empty?
  @attributes.empty?
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/rest_pki/object.rb', line 40

def respond_to?(name, include_all = false)
  return true if name.to_s.end_with? '='

  @attributes.has_key?(name.to_s) || super
end

#to_hashObject



34
35
36
37
38
# File 'lib/rest_pki/object.rb', line 34

def to_hash
  Hash[@attributes.map do |key, value|
    [ key, to_hash_value(value, :to_hash) ]
  end]
end

#unsaved_attributesObject



20
21
22
23
24
# File 'lib/rest_pki/object.rb', line 20

def unsaved_attributes
  Hash[@unsaved_attributes.map do |key|
    [ key, to_hash_value(self[key], :unsaved_attributes) ]
  end]
end