Module: Videojuicer::Resource::PropertyRegistry
Defined Under Namespace
Modules: SingletonMethods
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/videojuicer/resource/property_registry.rb', line 17
def self.included(base)
base.extend(SingletonMethods)
base.class_eval do
@attributes = {}
class << self
attr_accessor :attributes
end
end
base.property :id, Integer
end
|
.inherited(subclass) ⇒ Object
Allow subclasses of each resource to get the attributes accessor
30
31
32
33
|
# File 'lib/videojuicer/resource/property_registry.rb', line 30
def self.inherited(subclass)
v = "@attributes"
subclass.instance_variable_set(v, instance_variable_get(v))
end
|
Instance Method Details
#attr_get(key) ⇒ Object
61
62
63
64
|
# File 'lib/videojuicer/resource/property_registry.rb', line 61
def attr_get(key)
key = key.to_sym
attributes[key]
end
|
#attr_set(key, value) ⇒ Object
66
67
68
69
|
# File 'lib/videojuicer/resource/property_registry.rb', line 66
def attr_set(key, value)
key = key.to_sym
attributes[key] = coerce_value(key, value)
end
|
#attributes ⇒ Object
41
42
43
44
|
# File 'lib/videojuicer/resource/property_registry.rb', line 41
def attributes
@attributes ||= {}
@attributes
end
|
#attributes=(arg) ⇒ Object
46
47
48
49
50
51
|
# File 'lib/videojuicer/resource/property_registry.rb', line 46
def attributes=(arg)
raise ArgumentError, "Attributes must be set as a Hash" unless arg.is_a?(Hash)
arg.each do |key, value|
self.send("#{key}=", value)
end
end
|
#coerce_value(key, value) ⇒ Object
Takes what is normally a string and coerces it into the correct object type for the attribute’s actual type.
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/videojuicer/resource/property_registry.rb', line 73
def coerce_value(key, value)
return value unless value
klass = self.class.attributes[key][:class]
if value.is_a?(String) and value.any?
if klass.kind_of?(Videojuicer::Resource::Types::Base)
return klass.new(value).dump
end
if klass.respond_to?(:parse)
return klass.parse(value) rescue raise "Invalid date: #{value.inspect}"
end
elsif value.is_a? Hash and value.any?
if klass == DateTime
if value.is_a?(Hash)
year = value[:year]
month = value[:month]
day = value[:day]
hour = value[:hour] or "00"
minute = value[:minute] or "00"
value = klass.parse("#{year}-#{month}-#{day}T#{hour}:#{minute}:00+00:00")
else
raise ArgumentError, "Please supply a DateTime, Hash keyed w/ [:day, :month, :year, :hour, :minute] or a String that can be coerced into a date"
end
end
end
return value
end
|
#default_attributes ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/videojuicer/resource/property_registry.rb', line 53
def default_attributes
d = {}
self.class.attributes.each do |key, props|
d[key] = props[:default] || nil
end
return d
end
|
#initialize(attrs = {}) ⇒ Object
36
37
38
39
|
# File 'lib/videojuicer/resource/property_registry.rb', line 36
def initialize(attrs={})
self.attributes = default_attributes
self.attributes = attrs
end
|
#returnable_attributes ⇒ Object
Returns a hash of the attributes for this object, minus the private attributes that should not be included in the response.
105
106
107
108
109
110
111
112
|
# File 'lib/videojuicer/resource/property_registry.rb', line 105
def returnable_attributes
attrs = attributes.dup
self.class.attributes.select {|name, props| props[:writer] != :public}.each do |name, props|
attrs.delete name
end
attrs.delete(:id)
attrs
end
|