Class: ChartMogul::Object
- Inherits:
-
Object
- Object
- ChartMogul::Object
show all
- Defined in:
- lib/chartmogul/object.rb
Direct Known Subclasses
APIResource, Invoice, LineItems::OneTime, LineItems::Subscription, Metrics::ARPA, Metrics::ARR, Metrics::ASP, Metrics::Activity, Metrics::AllKeyMetric, Metrics::CustomerChurnRate, Metrics::CustomerCount, Metrics::LTV, Metrics::MRR, Metrics::MRRChurnRate, Metrics::Subscription, Summary
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Object
Returns a new instance of Object.
63
64
65
|
# File 'lib/chartmogul/object.rb', line 63
def initialize(attributes = {})
assign_writeable_attributes(attributes)
end
|
Class Method Details
.attributes ⇒ Object
6
7
8
|
# File 'lib/chartmogul/object.rb', line 6
def attributes
@attributes ||= Set.new
end
|
.define_private_writer(attribute, type) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/chartmogul/object.rb', line 39
def define_private_writer(attribute, type)
if type == :time
define_method("set_#{attribute}") do |value|
instance_variable_set("@#{attribute}", value && Time.parse(value))
end
elsif type == :date
define_method("set_#{attribute}") do |value|
instance_variable_set("@#{attribute}", value && Date.strptime(value, '%Y-%m-%d'))
end
else
define_method("set_#{attribute}") do |value|
instance_variable_set("@#{attribute}", value)
end
end
private "set_#{attribute}"
end
|
.define_reader(attribute, default) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/chartmogul/object.rb', line 29
def define_reader(attribute, default)
define_method(attribute) do
if instance_variable_defined?("@#{attribute}")
instance_variable_get("@#{attribute}")
else
instance_variable_set("@#{attribute}", default)
end
end
end
|
.define_writer(attribute) ⇒ Object
56
57
58
59
60
|
# File 'lib/chartmogul/object.rb', line 56
def define_writer(attribute)
define_method("#{attribute}=") do |value|
instance_variable_set("@#{attribute}", value)
end
end
|
.new_from_json(attributes = {}) ⇒ Object
67
68
69
70
71
|
# File 'lib/chartmogul/object.rb', line 67
def self.new_from_json(attributes={})
self.new.tap do |resource|
resource.assign_all_attributes(attributes)
end
end
|
.readonly_attr(attribute, options = {}) ⇒ Object
14
15
16
17
18
|
# File 'lib/chartmogul/object.rb', line 14
def readonly_attr(attribute, options = {})
attributes << attribute.to_sym
define_reader(attribute, options[:default])
define_private_writer(attribute, options[:type])
end
|
.writeable_attr(attribute, options = {}) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/chartmogul/object.rb', line 20
def writeable_attr(attribute, options = {})
attributes << attribute.to_sym
writeable_attributes << attribute.to_sym
define_reader(attribute, options[:default])
define_writer(attribute)
define_private_writer(attribute, options[:type])
end
|
.writeable_attributes ⇒ Object
10
11
12
|
# File 'lib/chartmogul/object.rb', line 10
def writeable_attributes
@writeable_attributes ||= Set.new
end
|
Instance Method Details
#allowed_for_write?(serialized_value) ⇒ Boolean
102
103
104
105
106
107
|
# File 'lib/chartmogul/object.rb', line 102
def allowed_for_write?(serialized_value)
return false if serialized_value.is_a?(Array) && serialized_value.empty?
return false if serialized_value.nil?
true
end
|
#assign_all_attributes(new_values) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/chartmogul/object.rb', line 87
def assign_all_attributes(new_values)
self.class.attributes.each do |attr|
self.send("set_#{attr}", new_values[attr]) if new_values.key?(attr)
end
self
end
|
#assign_writeable_attributes(new_values) ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/chartmogul/object.rb', line 79
def assign_writeable_attributes(new_values)
self.class.writeable_attributes.each do |attr, value|
self.send("#{attr}=", new_values[attr]) if new_values.key?(attr)
end
self
end
|
#instance_attributes ⇒ Object
73
74
75
76
77
|
# File 'lib/chartmogul/object.rb', line 73
def instance_attributes
self.class.attributes.each_with_object({}) do |attribute, hash|
hash[attribute] = send(attribute)
end
end
|
#serialize_for_write ⇒ Object
95
96
97
98
99
100
|
# File 'lib/chartmogul/object.rb', line 95
def serialize_for_write
self.class.writeable_attributes.each_with_object({}) do |attr, attrs|
serialized_value = serialized_value_for_attr(attr)
attrs[attr] = serialized_value if allowed_for_write?(serialized_value)
end
end
|
#serialized_value_for_attr(attr) ⇒ Object
109
110
111
112
113
114
115
116
117
|
# File 'lib/chartmogul/object.rb', line 109
def serialized_value_for_attr(attr)
serialize_method_name = "serialize_#{attr}"
if respond_to?(serialize_method_name)
send(serialize_method_name)
else
send(attr)
end
end
|