Class: Tumblr::Object
- Inherits:
-
Object
show all
- Defined in:
- lib/tumblr/object.rb
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.page_size ⇒ Object
Returns the value of attribute page_size.
4
5
6
|
# File 'lib/tumblr/object.rb', line 4
def page_size
@page_size
end
|
Class Method Details
.array_attribute(*args) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/tumblr/object.rb', line 61
def array_attribute(*args)
options = args.
attr_reader *args
klass = systematize(options[:object]) if options[:object]
args.each do |arg|
attributes << arg
define_method "#{arg}=" do |value|
if klass
set_attribute_value(arg, value.to_a.collect { |params| klass.build(params) })
else
set_attribute_value(arg, value.to_a)
end
end
end
end
|
.attributes ⇒ Object
47
48
49
|
# File 'lib/tumblr/object.rb', line 47
def attributes
@attributes ||= Set.new
end
|
.boolean_attribute(*args) ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/tumblr/object.rb', line 77
def boolean_attribute(*args)
attr_reader *args
args.each do |arg|
attributes << arg
define_method "#{arg}=" do |value|
set_attribute_value(arg, !!value)
end
end
end
|
.build(attributes) ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/tumblr/object.rb', line 37
def build(attributes)
object = new
attributes.each do |attribute, value|
if object.respond_to?("#{attribute}=")
object.send("#{attribute}=", value)
end
end
object
end
|
.date_attribute(*args) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/tumblr/object.rb', line 98
def date_attribute(*args)
attr_reader *args
args.each do |arg|
attributes << arg
define_method "#{arg}=" do |value|
value = Time.at(value) if value.kind_of?(Fixnum)
value = Date.parse(value) rescue nil if value.kind_of?(String)
value ||= value
set_attribute_value(arg, value)
end
end
end
|
.numeric_attribute(*args) ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/tumblr/object.rb', line 87
def numeric_attribute(*args)
attr_reader *args
args.each do |arg|
attributes << arg
define_method "#{arg}=" do |value|
value = (value.to_i == value.to_f) ? value.to_i : value.to_f
set_attribute_value(arg, value)
end
end
end
|
.object_attribute(*args) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/tumblr/object.rb', line 111
def object_attribute(*args)
attr_reader *args
options = args.
klass = systematize(options[:object]) if options[:object]
args.each do |arg|
attributes << arg
define_method "#{arg}=" do |value|
set_attribute_value(arg, klass.build(value))
end
end
end
|
.objectify(hash) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/tumblr/object.rb', line 20
def objectify(hash)
objectified = {}
hash.keys.each do |key|
klass = systematize(key)
if klass
if hash[key].kind_of?(Array)
objectified[key] = hash[key].collect { |value| klass.build(value) }
else
objectified[key] = klass.build(hash[key])
end
else
objectified[key] = hash[key]
end
end
objectified
end
|
.string_attribute(*args) ⇒ Object
51
52
53
54
55
56
57
58
59
|
# File 'lib/tumblr/object.rb', line 51
def string_attribute(*args)
attr_reader *args
args.each do |arg|
attributes << arg
define_method "#{arg}=" do |value|
set_attribute_value(arg, value.to_s)
end
end
end
|
.systematize(string) ⇒ Object
15
16
17
18
|
# File 'lib/tumblr/object.rb', line 15
def systematize(string)
string = string.to_s.gsub(/^Tumblr::/, '')
"Tumblr::#{string.singularize.classify}".constantize rescue nil
end
|
.unserialize(value) ⇒ Object
10
11
12
13
|
# File 'lib/tumblr/object.rb', line 10
def unserialize(value)
value = JSON.parse(value)
systematize(value.first).build(value.last)
end
|
Instance Method Details
144
145
146
147
148
149
150
|
# File 'lib/tumblr/object.rb', line 144
def as_json
json = {}
self.class.attributes.each do |key|
json[key] = send(key)
end
json
end
|
132
133
134
|
# File 'lib/tumblr/object.rb', line 132
def commit
storage.value = serialize
end
|
#dom_class ⇒ Object
136
137
138
|
# File 'lib/tumblr/object.rb', line 136
def dom_class
self.class.to_s.split("::").last.underscore
end
|
140
141
142
|
# File 'lib/tumblr/object.rb', line 140
def etag
Digest::SHA1.hexdigest(to_json)
end
|
#serialize ⇒ Object
124
125
126
|
# File 'lib/tumblr/object.rb', line 124
def serialize
[self.class.to_s, as_json].to_json
end
|
128
129
130
|
# File 'lib/tumblr/object.rb', line 128
def storage
@_storage ||= Redis::Value.new("#{Blog::STORAGE_KEY}:#{slug}", Tumblr.config.redis)
end
|
#to_json(force = false) ⇒ Object
152
153
154
155
|
# File 'lib/tumblr/object.rb', line 152
def to_json(force=false)
@json = nil if force
@json ||= JSON.generate(as_json)
end
|