morph 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.2.3. added Morph.from_hash method
2
+
1
3
  v0.2.2. changed rubygem requirement to be >= 1.2 instead of = 1.2
2
4
 
3
5
  v0.2.1. better accomodation of unicode method names
data/Manifest CHANGED
@@ -4,7 +4,7 @@ examples/hubbit.rb
4
4
  lib/morph.rb
5
5
  LICENSE
6
6
  README
7
- spec/morph_spec.rb
7
+ spec/lib/morph_spec.rb
8
8
  spec/morph_spec_helper.rb
9
9
  spec/spec.opts
10
10
  Manifest
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ begin
11
11
  m.description = File.readlines("README").first
12
12
  m.rubyforge_name = "morph"
13
13
  m.rdoc_options << '--inline-source'
14
+ m.dependencies = ["activesupport >=2.0.2"]
14
15
  m.rdoc_pattern = ["README", "CHANGELOG", "LICENSE"]
15
16
  end
16
17
 
data/lib/morph.rb CHANGED
@@ -1,5 +1,50 @@
1
+ require 'activesupport'
2
+
1
3
  module Morph
2
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
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
34
+
35
+ def self.from_hash hash
36
+ if hash.keys.size == 1
37
+ key = hash.keys.first
38
+ object = object_from_key key
39
+ if hash[key].is_a? Hash
40
+ attributes = hash[key]
41
+ add_to_object object, attributes
42
+ end
43
+ object
44
+ else
45
+ raise 'hash must have single key'
46
+ end
47
+ end
3
48
 
4
49
  def self.included(base)
5
50
  base.extend ClassMethods
data/morph.gemspec CHANGED
@@ -1,83 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
1
2
 
2
- # Gem::Specification for Morph-0.2.2
3
- # Originally generated by Echoe
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{morph}
5
+ s.version = "0.2.3"
4
6
 
5
- --- !ruby/object:Gem::Specification
6
- name: morph
7
- version: !ruby/object:Gem::Version
8
- version: 0.2.2
9
- platform: ruby
10
- authors:
11
- - Rob McKinnon
12
- autorequire:
13
- bindir: bin
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Rob McKinnon"]
9
+ s.date = %q{2009-03-14}
10
+ s.description = %q{Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.}
11
+ s.email = ["rob ~@nospam@~ rubyforge.org"]
12
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README"]
13
+ s.files = ["CHANGELOG", "examples/forger.rb", "examples/hubbit.rb", "lib/morph.rb", "LICENSE", "README", "spec/lib/morph_spec.rb", "spec/morph_spec_helper.rb", "spec/spec.opts", "Manifest", "morph.gemspec", "Rakefile"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Morph", "--main", "README", "--inline-source"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{morph}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.}
14
21
 
15
- date: 2008-10-08 00:00:00 +01:00
16
- default_executable:
17
- dependencies:
18
- - !ruby/object:Gem::Dependency
19
- name: echoe
20
- type: :development
21
- version_requirement:
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: "0"
27
- version:
28
- description: Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.
29
- email:
30
- - rob ~@nospam@~ rubyforge.org
31
- executables: []
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
32
25
 
33
- extensions: []
34
-
35
- extra_rdoc_files:
36
- - CHANGELOG
37
- - LICENSE
38
- - README
39
- files:
40
- - CHANGELOG
41
- - examples/forger.rb
42
- - examples/hubbit.rb
43
- - lib/morph.rb
44
- - LICENSE
45
- - README
46
- - spec/morph_spec.rb
47
- - spec/morph_spec_helper.rb
48
- - spec/spec.opts
49
- - Manifest
50
- - morph.gemspec
51
- - Rakefile
52
- has_rdoc: true
53
- homepage: ""
54
- post_install_message:
55
- rdoc_options:
56
- - --line-numbers
57
- - --inline-source
58
- - --title
59
- - Morph
60
- - --main
61
- - README
62
- - --inline-source
63
- require_paths:
64
- - lib
65
- required_ruby_version: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: "0"
70
- version:
71
- required_rubygems_version: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: "1.2"
76
- version:
77
- requirements: []
78
-
79
- rubyforge_project: morph
80
- rubygems_version: 1.3.0
81
- specification_version: 2
82
- summary: Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.
83
- test_files: []
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.0.2"])
28
+ else
29
+ s.add_dependency(%q<activesupport>, [">= 2.0.2"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<activesupport>, [">= 2.0.2"])
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
- require File.dirname(__FILE__) + '/../lib/morph'
2
- require File.dirname(__FILE__) + '/morph_spec_helper'
1
+ require File.dirname(__FILE__) + '/../../lib/morph'
2
+ require File.dirname(__FILE__) + '/../morph_spec_helper'
3
3
 
4
4
  describe Morph do
5
5
  describe "when writer method that didn't exist before is called with non-nil value" do
@@ -350,4 +350,59 @@ describe Morph do
350
350
  check_convert_to_morph_method_name '年龄', '年龄'
351
351
  end
352
352
  end
353
+
354
+ describe 'creating from hash' do
355
+ it 'should create classes and object instances' do
356
+ h = {
357
+ "CompanyDetails"=> {
358
+ "RegAddress"=> {
359
+ "AddressLine"=>["ST DAVID'S HOUSE", "WEST WING", "WOOD STREET", "CARDIFF CF10 1ES"]},
360
+ "LastFullMemDate"=>"2002-03-25",
361
+ "xsi:schemaLocation"=>"xmlgwdev.companieshouse.gov.uk/v1-0/schema/CompanyDetails.xsd",
362
+ "HasBranchInfo"=>"0",
363
+ "Mortgages"=> {
364
+ "NumMortSatisfied"=>"0",
365
+ "MortgageInd"=>"LT300",
366
+ "NumMortOutstanding"=>"7",
367
+ "NumMortPartSatisfied"=>"0",
368
+ "NumMortCharges"=>"7"},
369
+ "CompanyCategory"=>"Public Limited Company",
370
+ "HasAppointments"=>"1",
371
+ "SICCodes"=> {
372
+ "SicText"=>"stadiums"},
373
+ "Returns"=> {
374
+ "Overdue"=>"NO",
375
+ "DocumentAvailable"=>"1",
376
+ "NextDueDate"=>"2003-04-22",
377
+ "LastMadeUpDate"=>"2002-03-25"
378
+ },
379
+ "CountryOfOrigin"=>"United Kingdom",
380
+ "CompanyStatus"=>"Active",
381
+ "CompanyName"=>"MILLENNIUM STADIUM PLC",
382
+ "InLiquidation"=>"0",
383
+ "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance",
384
+ "Accounts"=>{
385
+ "Overdue"=>"NO",
386
+ "DocumentAvailable"=>"1",
387
+ "AccountCategory"=>"FULL",
388
+ "NextDueDate"=>"2002-11-30",
389
+ "LastMadeUpDate"=>"2001-04-30",
390
+ "AccountRefDate"=>"0000-30-04"},
391
+ "IncorporationDate"=>"1996-03-25",
392
+ "CompanyNumber"=>"03176906",
393
+ "xmlns"=>"http://xmlgw.companieshouse.gov.uk/v1-0"}
394
+ }
395
+ company_details = Morph.from_hash(h)
396
+ company_details.class.name.should == 'CompanyDetails'
397
+ company_details.class.morph_methods.include?('last_full_mem_date').should be_true
398
+ company_details.class.morph_methods.include?('accounts').should be_true
399
+
400
+ company_details.accounts.class.name.should == 'Accounts'
401
+ company_details.accounts.overdue.should == 'NO'
402
+ company_details.last_full_mem_date.should == "2002-03-25"
403
+ company_details.sic_codes.sic_text.should == 'stadiums'
404
+ company_details.reg_address.address_lines.should == ["ST DAVID'S HOUSE", "WEST WING", "WOOD STREET", "CARDIFF CF10 1ES"]
405
+ puts company_details.to_yaml
406
+ end
407
+ end
353
408
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob McKinnon
@@ -9,18 +9,18 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-08 00:00:00 +01:00
12
+ date: 2009-03-14 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: echoe
17
- type: :development
16
+ name: activesupport
17
+ type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
23
+ version: 2.0.2
24
24
  version:
25
25
  description: Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.
26
26
  email:
@@ -40,7 +40,7 @@ files:
40
40
  - lib/morph.rb
41
41
  - LICENSE
42
42
  - README
43
- - spec/morph_spec.rb
43
+ - spec/lib/morph_spec.rb
44
44
  - spec/morph_spec_helper.rb
45
45
  - spec/spec.opts
46
46
  - Manifest
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  requirements: []
75
75
 
76
76
  rubyforge_project: morph
77
- rubygems_version: 1.3.0
77
+ rubygems_version: 1.3.1
78
78
  signing_key:
79
79
  specification_version: 2
80
80
  summary: Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.