Class: Tosuto::Resource
- Inherits:
-
Object
show all
- Defined in:
- lib/tosuto/resource.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.attr_collections(hash) ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/tosuto/resource.rb', line 18
def self.attr_collections(hash)
hash.each do |name, klass|
attr_reader(name)
define_method("#{name}=") { |ary|
instance_variable_set("@#{name}", parse_collection(ary, klass))
}
end
end
|
.attr_objects(hash) ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/tosuto/resource.rb', line 9
def self.attr_objects(hash)
hash.each do |name, klass|
attr_reader(name)
define_method("#{name}=") { |obj|
instance_variable_set("@#{name}", parse_object(obj, klass))
}
end
end
|
5
6
7
|
# File 'lib/tosuto/resource.rb', line 5
def self.encode_form_data(body)
body.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join("&")
end
|
Instance Method Details
#inspect ⇒ Object
63
64
65
|
# File 'lib/tosuto/resource.rb', line 63
def inspect
"#<#{self.class} #{[@guid, @name || @display_name].join(" ")}>"
end
|
#parse_collection(ary, klass) ⇒ Object
49
50
51
52
53
|
# File 'lib/tosuto/resource.rb', line 49
def parse_collection(ary, klass)
ary.map { |obj|
parse_object(obj, klass)
}
end
|
#parse_object(obj, klass) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/tosuto/resource.rb', line 55
def parse_object(obj, klass)
if obj.is_a?(klass)
obj
else
klass.new(obj)
end
end
|
#set_attribute(key, value) ⇒ Object
36
37
38
39
|
# File 'lib/tosuto/resource.rb', line 36
def set_attribute(key, value)
setter = "#{underscore(key)}="
send(setter, value) if respond_to?(setter)
end
|
#set_attributes(hash) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/tosuto/resource.rb', line 41
def set_attributes(hash)
return unless hash.is_a?(Hash)
hash.each do |key, value|
set_attribute(key, value)
end
end
|
#underscore(value) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/tosuto/resource.rb', line 27
def underscore(value)
return value unless /[A-Z-]/.match?(value)
word = value.to_s.gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
|