morph 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/lib/morph.rb +45 -33
- data/morph.gemspec +1 -1
- data/spec/lib/morph_spec.rb +25 -5
- metadata +1 -1
data/CHANGELOG
CHANGED
data/lib/morph.rb
CHANGED
@@ -1,44 +1,15 @@
|
|
1
1
|
require 'activesupport'
|
2
2
|
|
3
3
|
module Morph
|
4
|
-
VERSION = "0.2.
|
5
|
-
|
6
|
-
def self.object_from_key key
|
7
|
-
begin
|
8
|
-
name = key.to_s
|
9
|
-
type = name.constantize
|
10
|
-
rescue NameError => e
|
11
|
-
Object.const_set name, Class.new
|
12
|
-
type = name.constantize
|
13
|
-
type.send(:include, Morph)
|
14
|
-
end
|
15
|
-
object = type.new
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.add_to_object object, attributes
|
19
|
-
attributes.each do |key, value|
|
20
|
-
attribute = key.gsub(':',' ')
|
21
|
-
attribute = attribute.underscore
|
22
|
-
|
23
|
-
if value.is_a?(String)
|
24
|
-
object.morph(attribute, value)
|
25
|
-
elsif value.is_a?(Array)
|
26
|
-
object.morph(attribute.pluralize, value)
|
27
|
-
elsif value.is_a? Hash
|
28
|
-
child_object = object_from_key key
|
29
|
-
add_to_object child_object, value
|
30
|
-
object.morph(attribute, child_object)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
4
|
+
VERSION = "0.2.4"
|
34
5
|
|
35
|
-
def self.from_hash hash
|
6
|
+
def self.from_hash hash, namespace=Morph
|
36
7
|
if hash.keys.size == 1
|
37
8
|
key = hash.keys.first
|
38
|
-
object = object_from_key key
|
9
|
+
object = object_from_key key, namespace
|
39
10
|
if hash[key].is_a? Hash
|
40
11
|
attributes = hash[key]
|
41
|
-
add_to_object object, attributes
|
12
|
+
add_to_object object, attributes, namespace
|
42
13
|
end
|
43
14
|
object
|
44
15
|
else
|
@@ -52,6 +23,47 @@ module Morph
|
|
52
23
|
base.send(:include, MethodMissing)
|
53
24
|
end
|
54
25
|
|
26
|
+
private
|
27
|
+
def self.object_from_key key, namespace
|
28
|
+
begin
|
29
|
+
name = key.to_s
|
30
|
+
type = "#{namespace.name}::#{name}".constantize
|
31
|
+
rescue NameError => e
|
32
|
+
namespace.const_set name, Class.new
|
33
|
+
type = "#{namespace.name}::#{name}".constantize
|
34
|
+
type.send(:include, Morph)
|
35
|
+
end
|
36
|
+
object = type.new
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.add_to_object object, attributes, namespace
|
40
|
+
attributes.each do |key, value|
|
41
|
+
attribute = key.gsub(':',' ')
|
42
|
+
attribute = attribute.underscore
|
43
|
+
|
44
|
+
if value.is_a?(String)
|
45
|
+
object.morph(attribute, value)
|
46
|
+
elsif value.is_a?(Array)
|
47
|
+
array = value
|
48
|
+
if array.size > 0 && array.collect(&:class).uniq == [Hash]
|
49
|
+
array = array.collect do |hash|
|
50
|
+
child = object_from_key key.singularize, namespace
|
51
|
+
add_to_object child, hash, namespace
|
52
|
+
child
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
object.morph(attribute.pluralize, array)
|
57
|
+
elsif value.is_a? Hash
|
58
|
+
child_object = object_from_key key, namespace
|
59
|
+
add_to_object child_object, value, namespace
|
60
|
+
object.morph(attribute, child_object)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
public
|
66
|
+
|
55
67
|
module ClassMethods
|
56
68
|
|
57
69
|
@@adding_morph_method = Hash.new {|hash,klass| hash[klass] = false }
|
data/morph.gemspec
CHANGED
data/spec/lib/morph_spec.rb
CHANGED
@@ -390,19 +390,39 @@ describe Morph do
|
|
390
390
|
"AccountRefDate"=>"0000-30-04"},
|
391
391
|
"IncorporationDate"=>"1996-03-25",
|
392
392
|
"CompanyNumber"=>"03176906",
|
393
|
-
"xmlns"=>"http://xmlgw.companieshouse.gov.uk/v1-0"
|
393
|
+
"xmlns"=>"http://xmlgw.companieshouse.gov.uk/v1-0",
|
394
|
+
"SearchItems"=> [
|
395
|
+
{ "CompanyDate"=> '',
|
396
|
+
"CompanyIndexStatus"=> '',
|
397
|
+
"DataSet"=>"LIVE",
|
398
|
+
"CompanyName"=>"CANONGROVE LIMITED",
|
399
|
+
"CompanyNumber"=>"SC244777" },
|
400
|
+
{ "CompanyDate"=>"",
|
401
|
+
"CompanyIndexStatus"=>"",
|
402
|
+
"DataSet"=>"LIVE",
|
403
|
+
"CompanyName"=>"CANONHALL ACCOUNTANCY LTD",
|
404
|
+
"CompanyNumber"=>"05110715" }
|
405
|
+
]
|
406
|
+
}
|
394
407
|
}
|
395
|
-
|
396
|
-
|
408
|
+
Object.const_set 'Company', Module.new
|
409
|
+
Company.const_set 'House', Module.new
|
410
|
+
|
411
|
+
company_details = Morph.from_hash(h, Company::House)
|
412
|
+
company_details.class.name.should == 'Company::House::CompanyDetails'
|
397
413
|
company_details.class.morph_methods.include?('last_full_mem_date').should be_true
|
398
414
|
company_details.class.morph_methods.include?('accounts').should be_true
|
399
415
|
|
400
|
-
company_details.accounts.class.name.should == 'Accounts'
|
416
|
+
company_details.accounts.class.name.should == 'Company::House::Accounts'
|
401
417
|
company_details.accounts.overdue.should == 'NO'
|
402
418
|
company_details.last_full_mem_date.should == "2002-03-25"
|
403
419
|
company_details.sic_codes.sic_text.should == 'stadiums'
|
404
420
|
company_details.reg_address.address_lines.should == ["ST DAVID'S HOUSE", "WEST WING", "WOOD STREET", "CARDIFF CF10 1ES"]
|
405
|
-
|
421
|
+
|
422
|
+
company_details.search_items.first.class.name.should == 'Company::House::SearchItem'
|
423
|
+
company_details.search_items.first.data_set.should == 'LIVE'
|
424
|
+
company_details.search_items.first.company_name.should == 'CANONGROVE LIMITED'
|
425
|
+
# puts company_details.to_yaml
|
406
426
|
end
|
407
427
|
end
|
408
428
|
end
|