Class: Zipmark::Entity
- Inherits:
-
Object
show all
- Defined in:
- lib/zipmark/entity.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Entity
Returns a new instance of Entity.
5
6
7
8
9
10
11
12
|
# File 'lib/zipmark/entity.rb', line 5
def initialize(options={})
@errors = {}
@attributes = Util.stringify_keys(options)
@dirty_attributes = {}
@client = @attributes.delete("client")
@resource_type = @attributes.delete("resource_type")
set_links
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/zipmark/entity.rb', line 36
def method_missing(meth, *args, &block)
if meth =~ /=$/
dirty_attributes[meth.to_s.sub(/=$/, '')] = args.first
else
dirty_attributes[meth.to_s] || attributes[meth.to_s]
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/zipmark/entity.rb', line 3
def attributes
@attributes
end
|
#client ⇒ Object
Returns the value of attribute client.
3
4
5
|
# File 'lib/zipmark/entity.rb', line 3
def client
@client
end
|
#dirty_attributes ⇒ Object
Returns the value of attribute dirty_attributes.
3
4
5
|
# File 'lib/zipmark/entity.rb', line 3
def dirty_attributes
@dirty_attributes
end
|
#errors ⇒ Object
Returns the value of attribute errors.
3
4
5
|
# File 'lib/zipmark/entity.rb', line 3
def errors
@errors
end
|
#links ⇒ Object
Returns the value of attribute links.
3
4
5
|
# File 'lib/zipmark/entity.rb', line 3
def links
@links
end
|
#resource_type ⇒ Object
Returns the value of attribute resource_type.
3
4
5
|
# File 'lib/zipmark/entity.rb', line 3
def resource_type
@resource_type
end
|
Instance Method Details
#apply_response(response) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/zipmark/entity.rb', line 54
def apply_response(response)
object = JSON.parse(response.body)
if client.successful?(response)
@attributes = object[resource_type]
set_links
elsif client.validation_error?(response)
@errors = object["errors"]
else
raise Zipmark::Error.new(object)
end
end
|
#created_at ⇒ Object
74
75
76
|
# File 'lib/zipmark/entity.rb', line 74
def created_at
Time.parse(attributes["created_at"]) if attributes["created_at"]
end
|
#id ⇒ Object
28
29
30
|
# File 'lib/zipmark/entity.rb', line 28
def id
attributes["id"]
end
|
#indentifier ⇒ Object
32
33
34
|
# File 'lib/zipmark/entity.rb', line 32
def indentifier
attributes['identifier']
end
|
#inspect ⇒ Object
24
25
26
|
# File 'lib/zipmark/entity.rb', line 24
def inspect
"<Entity:#{object_id} #{attributes.inspect}>"
end
|
#save ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/zipmark/entity.rb', line 44
def save
if links["self"]
response = client.put(links["self"].href, resource_type => dirty_attributes)
else
response = client.post("/#{resource_type}s", resource_type => attributes)
end
apply_response(response)
return self
end
|
#set_links ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/zipmark/entity.rb', line 14
def set_links
@links = {}
object_links = @attributes.delete("links")
if object_links.kind_of?(Array)
object_links.each do |link|
@links[link["rel"]] = Link.new(link)
end
end
end
|
#updated_at ⇒ Object
70
71
72
|
# File 'lib/zipmark/entity.rb', line 70
def updated_at
Time.parse(attributes["updated_at"]) if attributes["updated_at"]
end
|
#valid? ⇒ Boolean
66
67
68
|
# File 'lib/zipmark/entity.rb', line 66
def valid?
!!((id ||identifier) && errors.empty?)
end
|