Module: Morph
Defined Under Namespace
Modules: ClassMethods, MethodMissing
Constant Summary
collapse
- VERSION =
'0.5.0'
Class Method Summary
collapse
-
.classes ⇒ Object
-
.from_csv(csv, class_name, namespace = Morph) ⇒ Object
-
.from_hash(hash, namespace = Morph) ⇒ Object
-
.from_json(json, root_key = nil, namespace = Morph) ⇒ Object
-
.from_tsv(tsv, class_name, namespace = Morph) ⇒ Object
-
.from_xml(xml, namespace = Morph) ⇒ Object
-
.generate_migrations(object, options = {}) ⇒ Object
-
.included(base) ⇒ Object
-
.register_listener(listener) ⇒ Object
-
.script_generate(morphed_class, options = {}) ⇒ Object
-
.unregister_listener(listener) ⇒ Object
Instance Method Summary
collapse
Class Method Details
.classes ⇒ Object
138
139
140
|
# File 'lib/morph.rb', line 138
def classes
Chas.morph_classes
end
|
.from_csv(csv, class_name, namespace = Morph) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/morph.rb', line 158
def from_csv csv, class_name, namespace=Morph
objects = []
if !(RUBY_VERSION >= "1.9")
begin
require 'fastercsv'
rescue LoadError
puts "\nYou need to install the fastercsv gem to use Morph.from_csv() with Ruby 1.8"
puts " gem install fastercsv\n"
end
end
csv_utility = (RUBY_VERSION >= "1.9") ? CSV : 'FasterCSV'.constantize
csv_utility.parse(csv, { :headers => true }) do |row|
object = object_from_name class_name, namespace
row.each do |key, value|
object.morph(key, value)
end
objects << object
end
objects
end
|
.from_hash(hash, namespace = Morph) ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/morph.rb', line 208
def from_hash hash, namespace=Morph
if hash.keys.size == 1
name = hash.keys.first
case hash[name]
when Hash
object_from_hash hash[name], name, namespace
when Array
objects_from_array hash[name], name, namespace
else
raise 'hash root value must be a Hash or an Array'
end
else
raise 'hash must have single key'
end
end
|
.from_json(json, root_key = nil, namespace = Morph) ⇒ Object
201
202
203
204
205
206
|
# File 'lib/morph.rb', line 201
def from_json json, root_key=nil, namespace=Morph
require 'json' unless defined? JSON
hash = JSON.parse json
hash = { root_key => hash } if root_key
from_hash hash, namespace
end
|
.from_tsv(tsv, class_name, namespace = Morph) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/morph.rb', line 180
def from_tsv tsv, class_name, namespace=Morph
lines = tsv.split("\n")
attributes = lines[0].split("\t")
lines = lines[1..(lines.length-1)]
objects = []
lines.each do |line|
values = line.split("\t")
object = object_from_name class_name, namespace
attributes.each_with_index do |attribute, index|
object.morph(attribute, values[index])
end
objects << object
end
objects
end
|
.from_xml(xml, namespace = Morph) ⇒ Object
196
197
198
199
|
# File 'lib/morph.rb', line 196
def from_xml xml, namespace=Morph
hash = Hash.from_xml xml
from_hash hash, namespace
end
|
.generate_migrations(object, options = {}) ⇒ Object
150
151
152
153
154
155
156
|
# File 'lib/morph.rb', line 150
def generate_migrations object, options={}
options[:ignore] ||= []
options[:belongs_to_id] ||= ''
migrations = []
name = object.class.name.demodulize.underscore
add_migration name, object.morph_attributes, migrations, options
end
|
.included(base) ⇒ Object
234
235
236
237
|
# File 'lib/morph.rb', line 234
def included(base)
base.extend ClassMethods
base.send(:include, MethodMissing)
end
|
.register_listener(listener) ⇒ Object
142
143
144
|
# File 'lib/morph.rb', line 142
def register_listener listener
Chas.register_listener listener
end
|
.script_generate(morphed_class, options = {}) ⇒ Object
225
226
227
228
229
230
231
232
|
# File 'lib/morph.rb', line 225
def script_generate morphed_class, options={}
name = morphed_class.name.to_s.split('::').last
name = yield name if block_given?
generator = options[:generator] || 'model'
line = ["rails destroy #{generator} #{name}; rails generate #{generator} #{name}"]
morphed_class.morph_methods.select{|m| not(m =~ /=$/) }.each {|attribute| line << " #{attribute}:string"}
line.join('')
end
|
.unregister_listener(listener) ⇒ Object
146
147
148
|
# File 'lib/morph.rb', line 146
def unregister_listener listener
Chas.unregister_listener listener
end
|
Instance Method Details
#morph(attributes_or_label, value = nil) ⇒ Object
Set attribute value(s). Adds accessor methods to class if they are not already present.
Can be called with a string and a value, a symbol and a value, or with a hash of attribute to value pairs. For example.
require 'rubygems'; require 'morph'
class Order; include Morph; end
order = Order.new
order.morph :drink => 'tea', :sugars => 2, 'milk' => 'yes please'
order.morph 'Payment type:', 'will wash dishes'
order.morph :lemon, false
p order
@payment_type="will wash dishes", @sugars=2, @drink="tea">
376
377
378
379
380
381
382
383
|
# File 'lib/morph.rb', line 376
def morph attributes_or_label, value=nil
if attributes_or_label.is_a? Hash
attributes_or_label.each { |a, v| morph(a, v) }
else
attribute = Chas.convert_to_morph_method_name(attributes_or_label)
send("#{attribute}=".to_sym, value)
end
end
|
#morph_attributes ⇒ Object
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
# File 'lib/morph.rb', line 385
def morph_attributes
attributes = self.class.morph_attributes.inject({}) do |hash, attribute|
unless attribute =~ /=\Z/
symbol = attribute.to_sym
value = send(symbol)
value.each do |key, v|
value[key] = v.morph_attributes if v.respond_to?(:morph_attributes)
end if value.is_a? Hash
value = value.collect {|v| v.respond_to?(:morph_attributes) ? v.morph_attributes : v } if value.is_a? Array
value = value.morph_attributes if value.respond_to? :morph_attributes
hash[symbol] = value
end
hash
end
end
|