Class: ChartMogul::Object
- Inherits:
-
Object
- Object
- ChartMogul::Object
show all
- Defined in:
- lib/chartmogul/object.rb
Direct Known Subclasses
APIResource, LineItems::OneTime, LineItems::Subscription, Metrics::ARPA, Metrics::ARR, Metrics::ASP, Metrics::Activity, Metrics::AllKeyMetric, Metrics::CustomerChurnRate, Metrics::CustomerCount, Metrics::Customers::Activity, Metrics::Customers::Subscription, Metrics::LTV, Metrics::MRR, Metrics::MRRChurnRate, Summary, SummaryAll
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Object
Returns a new instance of Object.
65
66
67
|
# File 'lib/chartmogul/object.rb', line 65
def initialize(attributes = {})
assign_writeable_attributes(attributes)
end
|
Class Method Details
.attributes ⇒ Object
8
9
10
|
# File 'lib/chartmogul/object.rb', line 8
def attributes
@attributes ||= Set.new
end
|
.define_private_writer(attribute, type) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/chartmogul/object.rb', line 41
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
31
32
33
34
35
36
37
38
39
|
# File 'lib/chartmogul/object.rb', line 31
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
58
59
60
61
62
|
# File 'lib/chartmogul/object.rb', line 58
def define_writer(attribute)
define_method("#{attribute}=") do |value|
instance_variable_set("@#{attribute}", value)
end
end
|
.new_from_json(attributes = {}) ⇒ Object
69
70
71
72
73
|
# File 'lib/chartmogul/object.rb', line 69
def self.new_from_json(attributes = {})
new.tap do |resource|
resource.assign_all_attributes(attributes)
end
end
|
.readonly_attr(attribute, options = {}) ⇒ Object
16
17
18
19
20
|
# File 'lib/chartmogul/object.rb', line 16
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
22
23
24
25
26
27
28
29
|
# File 'lib/chartmogul/object.rb', line 22
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
12
13
14
|
# File 'lib/chartmogul/object.rb', line 12
def writeable_attributes
@writeable_attributes ||= Set.new
end
|
Instance Method Details
#allowed_for_write?(serialized_value) ⇒ Boolean
104
105
106
107
108
109
|
# File 'lib/chartmogul/object.rb', line 104
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
89
90
91
92
93
94
95
|
# File 'lib/chartmogul/object.rb', line 89
def assign_all_attributes(new_values)
self.class.attributes.each do |attr|
send("set_#{attr}", new_values[attr]) if new_values.key?(attr)
end
self
end
|
#assign_writeable_attributes(new_values) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/chartmogul/object.rb', line 81
def assign_writeable_attributes(new_values)
self.class.writeable_attributes.each do |attr, _value|
send("#{attr}=", new_values[attr]) if new_values.key?(attr)
end
self
end
|
#instance_attributes ⇒ Object
75
76
77
78
79
|
# File 'lib/chartmogul/object.rb', line 75
def instance_attributes
self.class.attributes.each_with_object({}) do |attribute, hash|
hash[attribute] = send(attribute)
end
end
|
#serialize_for_write ⇒ Object
97
98
99
100
101
102
|
# File 'lib/chartmogul/object.rb', line 97
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
111
112
113
114
115
116
117
118
119
|
# File 'lib/chartmogul/object.rb', line 111
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
|