Class: Shopify::ShopifyModel
- Inherits:
-
Object
- Object
- Shopify::ShopifyModel
show all
- Defined in:
- lib/shopify.rb,
lib/shopify/support.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(shop, attrs = {}) ⇒ ShopifyModel
Returns a new instance of ShopifyModel.
148
149
150
151
152
|
# File 'lib/shopify.rb', line 148
def initialize(shop, attrs={})
@new_record = true
attrs.each { |k,v| instance_variable_set("@#{k}", v) }
@shop = shop
end
|
Instance Attribute Details
#shop ⇒ Object
Returns the value of attribute shop.
153
154
155
|
# File 'lib/shopify.rb', line 153
def shop
@shop
end
|
Class Method Details
.children_of(parent_klass) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/shopify.rb', line 115
def children_of(parent_klass)
@parent = parent_klass
parent_klass_name = parent_klass.name.gsub(/.*::/,'')
klass_name = self.name.gsub(/.*::/,'')
parent_klass.class_eval "
def #{klass_name.snake_case.pluralize}(query_params={})
@#{klass_name.snake_case.pluralize} ||= begin
json = Shopify.get(\"/#{parent_klass_name.snake_case.pluralize}/\#{id}/#{klass_name.snake_case.pluralize}.xml\", shop.options(:query => query_params))
case
when json['#{parent_klass_name.snake_case}_#{klass_name.snake_case.pluralize}']
json['#{parent_klass_name.snake_case}_#{klass_name.snake_case.pluralize}']
when json['#{klass_name.snake_case.pluralize}']
json['#{klass_name.snake_case.pluralize}']
else
json
end
end
@#{klass_name.snake_case.pluralize} = (@#{klass_name.snake_case.pluralize}.is_a?(Array) ? @#{klass_name.snake_case.pluralize}.collect {|i| #{klass_name}.instantiate(shop, i)} : [#{klass_name}.instantiate(shop, @#{klass_name.snake_case.pluralize})]) unless @#{klass_name.snake_case.pluralize}.is_a?(#{klass_name}) || @#{klass_name.snake_case.pluralize}.is_a?(Array) && @#{klass_name.snake_case.pluralize}[0].is_a?(#{klass_name})
@#{klass_name.snake_case.pluralize}
end
"
end
|
.instantiate(shop, attrs = {}) ⇒ Object
145
146
147
|
# File 'lib/shopify.rb', line 145
def self.instantiate(shop, attrs={})
new(shop, attrs.merge('new_record' => false))
end
|
.is_child? ⇒ Boolean
140
141
142
|
# File 'lib/shopify.rb', line 140
def is_child?
(@parent ||= nil)
end
|
.load_api_classes(api) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/shopify.rb', line 53
def load_api_classes(api)
api.each_key do |klass_name|
next unless klass_name.is_a?(String)
mode = []
singular = klass_name.singular
mode = [:singular] if singular == klass_name
klass = Shopify.const_set(singular, Class.new(ShopifyModel))
self == ShopifyModel ?
klass.send(:top_level, *mode) :
klass.send(:children_of, self)
klass.send(:attr_accessor, *api[klass_name].delete(:properties).split(', ').map {|s| s.to_sym})
klass.load_api_classes(api[klass_name])
end
end
|
.top_level(*options) ⇒ Object
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
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/shopify.rb', line 74
def top_level(*options)
klass_name = self.name.gsub(/.*::/,'')
if options.include?(:singular)
Shopify.class_eval "
def #{klass_name.snake_case}(query_params={})
json = Shopify.get(\"/#{klass_name.snake_case}.xml\", options(:query => query_params))
begin
#{klass_name}.instantiate(self, json['#{klass_name.snake_case}'])
rescue => e
warn \"Error: \#{e.inspect}\"
json
end
end
"
else
Shopify.class_eval "
def #{klass_name.snake_case.pluralize}(query_params={})
json = Shopify.get(\"/#{klass_name.snake_case.pluralize}.xml\", options(:query => query_params))
if json['#{klass_name.snake_case.pluralize}']
json['#{klass_name.snake_case.pluralize}'].collect {|i| #{klass_name}.instantiate(self, i)}
else
json
end
end
def #{klass_name.snake_case}(id)
json = Shopify.get(\"/#{klass_name.snake_case.pluralize}/\#{id}.xml\", options)
if json['#{klass_name.snake_case}']
#{klass_name}.instantiate(self, json['#{klass_name.snake_case}'])
else
json
end
end
"
end
end
|
Instance Method Details
#inspect ⇒ Object
155
156
157
|
# File 'lib/shopify.rb', line 155
def inspect
"<#{self.class.name} shop=#{@shop.host} #{instance_variables.reject {|i| i=='@shop' || instance_variable_get(i).to_s==''}.map {|i| "#{i}=#{instance_variable_get(i).inspect}"}.join(' ')}>"
end
|