morph 0.2.7 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.2.8. added from_xml() and from_tsv(); updated for active_support; fixed from_hash() when hash root is an array
2
+
1
3
  v0.2.7. handle dash when converting to method name, reported by danwrong
2
4
 
3
5
  v0.2.6. handle more types of value types in from_hash()
data/Rakefile CHANGED
@@ -1,7 +1,13 @@
1
1
  require 'rubygems'
2
- require 'spec'
3
2
  require 'lib/morph'
4
3
 
4
+ begin
5
+ require 'spec'
6
+ rescue LoadError
7
+ puts "\nYou need to install the rspec gem to perform meta operations on this gem"
8
+ puts " sudo gem install rspec\n"
9
+ end
10
+
5
11
  begin
6
12
  require 'echoe'
7
13
 
@@ -16,7 +22,8 @@ begin
16
22
  end
17
23
 
18
24
  rescue LoadError
19
- puts "You need to install the echoe gem to perform meta operations on this gem"
25
+ puts "\nYou need to install the echoe gem to perform meta operations on this gem"
26
+ puts " sudo gem install echoe\n\n"
20
27
  end
21
28
 
22
29
  desc "Open an irb session preloaded with this library"
data/lib/morph.rb CHANGED
@@ -1,7 +1,19 @@
1
- require 'activesupport'
1
+ begin
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'active_support/inflector'
4
+ require 'active_support/core_ext/string/inflections'
5
+ require 'active_support/core_ext/xml_mini'
6
+ require 'active_support/core_ext/hash/conversions'
7
+ rescue Exception => e
8
+ begin
9
+ require 'active_support'
10
+ rescue Exception => e
11
+ require 'activesupport'
12
+ end
13
+ end
2
14
 
3
15
  module Morph
4
- VERSION = "0.2.7"
16
+ VERSION = "0.2.8"
5
17
 
6
18
  class << self
7
19
  def generate_migrations object, options={}
@@ -12,15 +24,43 @@ module Morph
12
24
  add_migration name, object.morph_attributes, migrations, options
13
25
  end
14
26
 
27
+ def from_tsv tsv, class_name, namespace=Morph
28
+ lines = tsv.split("\n")
29
+ attributes = lines[0].split("\t")
30
+ lines = lines[1..(lines.length-1)]
31
+ objects = []
32
+ lines.each do |line|
33
+ values = line.split("\t")
34
+ object = object_from_name class_name, namespace
35
+ attributes.each_with_index do |attribute, index|
36
+ object.morph(attribute, values[index])
37
+ end
38
+ objects << object
39
+ end
40
+ objects
41
+ end
42
+
43
+ def from_xml xml, namespace=Morph
44
+ hash = Hash.from_xml xml
45
+ from_hash hash, namespace
46
+ end
47
+
15
48
  def from_hash hash, namespace=Morph
16
49
  if hash.keys.size == 1
17
50
  key = hash.keys.first
18
- object = object_from_name key, namespace
51
+
19
52
  if hash[key].is_a? Hash
20
53
  attributes = hash[key]
54
+ object = object_from_name(key, namespace)
21
55
  add_to_object object, attributes, namespace
56
+ object
57
+ elsif hash[key].is_a? Array
58
+ array = hash[key]
59
+ name = key.to_s.singularize
60
+ objects_from_array(array, name, namespace)
61
+ else
62
+ raise 'hash root value must be a Hash or an Array'
22
63
  end
23
- object
24
64
  else
25
65
  raise 'hash must have single key'
26
66
  end
@@ -42,7 +82,7 @@ module Morph
42
82
  when String
43
83
  attribute_name = attribute.to_s
44
84
  unless options[:ignore].include?(attribute_name)
45
- type = attribute_name.ends_with?('date') ? 'date' : 'string'
85
+ type = attribute_name[/date$/] ? 'date' : 'string'
46
86
  attribute_def = "#{attribute}:#{type}"
47
87
  migration.sub!(migration, "#{migration} #{attribute_def}")
48
88
  end
@@ -92,10 +132,11 @@ module Morph
92
132
  attributes.each do |name, value|
93
133
  attribute = name.gsub(':',' ').underscore
94
134
  case value
95
- when String, Date
135
+ when String, Date, Time, TrueClass, FalseClass, Fixnum, Float
96
136
  object.morph(attribute, value)
97
137
  when Array
98
- object.morph(attribute.pluralize, objects_from_array(value, name, namespace))
138
+ attribute = attribute.pluralize
139
+ object.morph(attribute, objects_from_array(value, name, namespace))
99
140
  when Hash
100
141
  object.morph(attribute, object_from_hash(value, name, namespace))
101
142
  when NilClass
data/morph.gemspec CHANGED
@@ -2,26 +2,26 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{morph}
5
- s.version = "0.2.7"
5
+ s.version = "0.2.8"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Rob McKinnon"]
9
- s.date = %q{2009-04-13}
10
- s.description = %q{Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.}
9
+ s.date = %q{2010-02-09}
10
+ s.description = %q{Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.
11
+ }
11
12
  s.email = ["rob ~@nospam@~ rubyforge.org"]
12
13
  s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README"]
13
14
  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
15
  s.homepage = %q{}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Morph", "--main", "README", "--inline-source"]
17
17
  s.require_paths = ["lib"]
18
18
  s.rubyforge_project = %q{morph}
19
- s.rubygems_version = %q{1.3.1}
19
+ s.rubygems_version = %q{1.3.5}
20
20
  s.summary = %q{Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.}
21
21
 
22
22
  if s.respond_to? :specification_version then
23
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
- s.specification_version = 2
24
+ s.specification_version = 3
25
25
 
26
26
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
27
  s.add_runtime_dependency(%q<activesupport>, [">= 2.0.2"])
@@ -485,4 +485,85 @@ xmlns_xsi: http://www.w3.org/2001/XMLSchema-instance
485
485
  xsi_schema_location: xmlgwdev.companieshouse.gov.uk/v1-0/schema/CompanyDetails.xsd|
486
486
  end
487
487
  end
488
+
489
+ describe 'creating from xml' do
490
+
491
+ def check_councils councils, class_name
492
+ councils.class.should == Array
493
+ councils.size.should == 2
494
+ councils.first.class.name.should == class_name
495
+ councils.first.name.should == 'Aberdeen City Council'
496
+ councils.last.name.should == 'Allerdale Borough Council'
497
+ end
498
+
499
+ it 'should create classes and object instances' do
500
+ councils = Morph.from_xml(xml)
501
+ check_councils councils, 'Morph::Council'
502
+ end
503
+
504
+ describe 'when module name is supplied' do
505
+ it 'should create classes and object instances' do
506
+ Object.const_set 'Ppc', Module.new
507
+ councils = Morph.from_xml(xml, Ppc)
508
+ check_councils councils, 'Ppc::Council'
509
+ end
510
+ end
511
+
512
+ def xml
513
+ %Q[<?xml version="1.0" encoding="UTF-8"?>
514
+ <councils type="array">
515
+ <council>
516
+ <name>Aberdeen City Council</name>
517
+ </council>
518
+ <council>
519
+ <name>Allerdale Borough Council</name>
520
+ </council>
521
+ </councils>]
522
+ end
523
+ end
524
+
525
+ describe 'creating from tsv (tab separated value)' do
526
+
527
+ def check_councillors councillors, class_name
528
+ councillors.class.should == Array
529
+ councillors.size.should == 2
530
+ councillor = councillors.first
531
+ councillor.class.name.should == class_name
532
+ councillor.name.should == 'Ted Roe'
533
+ councillor.party.should == 'labour'
534
+ councillor.councillors.should == 'Councillor for Stretford Ward'
535
+ councillor.councils.should == 'Trafford Council'
536
+ councillor.respond_to?(:council_experience).should be_false
537
+
538
+ councillor = councillors.last
539
+ councillor.name.should == 'Ali Davidson'
540
+ councillor.party.should == 'labour'
541
+ councillor.councillors.should == ''
542
+ councillor.councils.should == 'Basildon District Council'
543
+ councillor.respond_to?(:council_experience).should be_false
544
+ end
545
+
546
+ describe 'when class name is supplied' do
547
+ it 'should create classes and object instances' do
548
+ councillors = Morph.from_tsv(tsv, 'Councillor')
549
+ check_councillors councillors, 'Morph::Councillor'
550
+ end
551
+ end
552
+
553
+ describe 'when class name and module name is supplied' do
554
+ it 'should create classes and object instances' do
555
+ Object.const_set 'Ppc', Module.new
556
+ councillors = Morph.from_tsv(tsv, 'Councillor', Ppc)
557
+ check_councillors councillors, 'Ppc::Councillor'
558
+ end
559
+ end
560
+
561
+ def tsv
562
+ %Q[name party councillors councils council_experience
563
+ Ted Roe labour Councillor for Stretford Ward Trafford Council
564
+ Ali Davidson labour Basildon District Council
565
+ ]
566
+ end
567
+ end
568
+
488
569
  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.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob McKinnon
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-13 00:00:00 +01:00
12
+ date: 2010-02-09 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,7 +22,9 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 2.0.2
24
24
  version:
25
- description: Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.
25
+ description: |
26
+ Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.
27
+
26
28
  email:
27
29
  - rob ~@nospam@~ rubyforge.org
28
30
  executables: []
@@ -48,6 +50,8 @@ files:
48
50
  - Rakefile
49
51
  has_rdoc: true
50
52
  homepage: ""
53
+ licenses: []
54
+
51
55
  post_install_message:
52
56
  rdoc_options:
53
57
  - --line-numbers
@@ -74,9 +78,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
78
  requirements: []
75
79
 
76
80
  rubyforge_project: morph
77
- rubygems_version: 1.3.1
81
+ rubygems_version: 1.3.5
78
82
  signing_key:
79
- specification_version: 2
83
+ specification_version: 3
80
84
  summary: Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.
81
85
  test_files: []
82
86