morph 0.2.9 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/Rakefile +0 -1
- data/lib/morph.rb +31 -11
- data/morph.gemspec +7 -7
- data/spec/lib/morph_spec.rb +39 -25
- data/spec/morph_spec_helper.rb +21 -4
- metadata +16 -9
data/CHANGELOG
CHANGED
data/Rakefile
CHANGED
data/lib/morph.rb
CHANGED
@@ -1,20 +1,31 @@
|
|
1
|
-
|
1
|
+
if RUBY_VERSION >= "1.9"
|
2
|
+
require 'csv'
|
3
|
+
else
|
4
|
+
begin
|
5
|
+
require 'fastercsv'
|
6
|
+
rescue LoadError
|
7
|
+
puts "\nYou need to install the fastercsv gem to use Morph.from_csv with Ruby 1.8"
|
8
|
+
puts " sudo gem install fastercsv\n"
|
9
|
+
end
|
10
|
+
end
|
2
11
|
begin
|
12
|
+
# require 'active_support'
|
3
13
|
require 'active_support/core_ext/object/blank'
|
4
14
|
require 'active_support/inflector'
|
5
15
|
require 'active_support/core_ext/string/inflections'
|
6
|
-
require 'active_support/core_ext/xml_mini'
|
7
16
|
require 'active_support/core_ext/hash/conversions'
|
8
17
|
rescue Exception => e
|
9
18
|
begin
|
19
|
+
puts e.to_s
|
10
20
|
require 'active_support'
|
11
21
|
rescue Exception => e
|
22
|
+
puts e.to_s
|
12
23
|
require 'activesupport'
|
13
24
|
end
|
14
25
|
end
|
15
26
|
|
16
27
|
module Morph
|
17
|
-
VERSION = "0.
|
28
|
+
VERSION = "0.3.0"
|
18
29
|
|
19
30
|
class << self
|
20
31
|
def generate_migrations object, options={}
|
@@ -27,13 +38,14 @@ module Morph
|
|
27
38
|
|
28
39
|
def from_csv csv, class_name, namespace=Morph
|
29
40
|
objects = []
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
41
|
+
csv_utility = (RUBY_VERSION >= "1.9") ? CSV : FasterCSV
|
42
|
+
csv_utility.parse(csv, { :headers => true }) do |row|
|
43
|
+
object = object_from_name class_name, namespace
|
44
|
+
row.each do |key, value|
|
45
|
+
object.morph(key, value)
|
46
|
+
end
|
47
|
+
objects << object
|
34
48
|
end
|
35
|
-
objects << object
|
36
|
-
end
|
37
49
|
objects
|
38
50
|
end
|
39
51
|
|
@@ -90,6 +102,7 @@ module Morph
|
|
90
102
|
migration = "./script/generate model #{name}#{options[:belongs_to_id]}"
|
91
103
|
options[:belongs_to_id] = ''
|
92
104
|
migrations << migration
|
105
|
+
attributes = [attributes] if attributes.is_a?(String)
|
93
106
|
attributes.to_a.sort{|a,b| a[0].to_s <=> b[0].to_s}.each do |attribute, value|
|
94
107
|
case value
|
95
108
|
when String
|
@@ -174,7 +187,11 @@ module Morph
|
|
174
187
|
end
|
175
188
|
|
176
189
|
def morph_methods
|
177
|
-
|
190
|
+
if RUBY_VERSION >= "1.9"
|
191
|
+
@@morph_methods[self].keys.sort.map(&:to_sym)
|
192
|
+
else
|
193
|
+
@@morph_methods[self].keys.sort
|
194
|
+
end
|
178
195
|
end
|
179
196
|
|
180
197
|
def adding_morph_method= true_or_false
|
@@ -217,7 +234,7 @@ module Morph
|
|
217
234
|
if @@morph_methods[self].has_key? symbol.to_s
|
218
235
|
@@morph_methods[self].delete symbol.to_s
|
219
236
|
is_writer = symbol.to_s =~ /=$/
|
220
|
-
@@morph_attributes[self].delete(symbol) unless is_writer
|
237
|
+
@@morph_attributes[self].delete(symbol) unless is_writer
|
221
238
|
end
|
222
239
|
end
|
223
240
|
|
@@ -282,6 +299,9 @@ module Morph
|
|
282
299
|
|
283
300
|
def morph_method_missing symbol, *args
|
284
301
|
attribute = symbol.to_s.chomp '='
|
302
|
+
if RUBY_VERSION >= "1.9"
|
303
|
+
attribute = attribute.to_sym
|
304
|
+
end
|
285
305
|
|
286
306
|
if Object.instance_methods.include?(attribute)
|
287
307
|
raise "'#{attribute}' is an instance_method on Object, cannot create accessor methods for '#{attribute}'"
|
data/morph.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{morph}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.3.0"
|
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{2010-
|
9
|
+
s.date = %q{2010-10-18}
|
10
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
12
|
s.email = ["rob ~@nospam@~ rubyforge.org"]
|
@@ -16,19 +16,19 @@ Gem::Specification.new do |s|
|
|
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.
|
19
|
+
s.rubygems_version = %q{1.3.7}
|
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
24
|
s.specification_version = 3
|
25
25
|
|
26
|
-
if Gem::Version.new(Gem::
|
27
|
-
s.add_runtime_dependency(%q<
|
26
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 2.0.2"])
|
28
28
|
else
|
29
|
-
s.add_dependency(%q<
|
29
|
+
s.add_dependency(%q<activesupport>, [">= 2.0.2"])
|
30
30
|
end
|
31
31
|
else
|
32
|
-
s.add_dependency(%q<
|
32
|
+
s.add_dependency(%q<activesupport>, [">= 2.0.2"])
|
33
33
|
end
|
34
34
|
end
|
data/spec/lib/morph_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require File.dirname(__FILE__) + '/../../lib/morph'
|
2
3
|
require File.dirname(__FILE__) + '/../morph_spec_helper'
|
3
4
|
|
@@ -59,8 +60,13 @@ describe Morph do
|
|
59
60
|
@morphed_class.morph_methods.size.should == 2
|
60
61
|
@another_morphed_class.morph_methods.size.should == 2
|
61
62
|
|
62
|
-
|
63
|
-
|
63
|
+
if RUBY_VERSION >= "1.9"
|
64
|
+
@morphed_class.morph_methods.should == [:every,:every=]
|
65
|
+
@another_morphed_class.morph_methods.should == [:no,:no=]
|
66
|
+
else
|
67
|
+
@morphed_class.morph_methods.should == ['every','every=']
|
68
|
+
@another_morphed_class.morph_methods.should == ['no','no=']
|
69
|
+
end
|
64
70
|
end
|
65
71
|
|
66
72
|
it 'should call morph_attributes on both objects, when one object has a reference to another' do
|
@@ -94,7 +100,7 @@ describe Morph do
|
|
94
100
|
attributes.delete(:every)
|
95
101
|
attributes = @morph.morph_attributes
|
96
102
|
attributes[:every].should == 'which'
|
97
|
-
|
103
|
+
|
98
104
|
attributes = @morph.class.morph_attributes
|
99
105
|
attributes.should == [:every, :loose]
|
100
106
|
attributes.delete(:every)
|
@@ -176,7 +182,7 @@ describe Morph do
|
|
176
182
|
|
177
183
|
describe "when morph method used to set unicode attribute name with a value" do
|
178
184
|
before :each do
|
179
|
-
$KCODE = "u"
|
185
|
+
$KCODE = "u" unless RUBY_VERSION >= "1.9"
|
180
186
|
remove_morph_methods
|
181
187
|
@age = 19
|
182
188
|
@attribute = "年龄"
|
@@ -185,7 +191,7 @@ describe Morph do
|
|
185
191
|
end
|
186
192
|
|
187
193
|
after :all do
|
188
|
-
$KCODE = "NONE"
|
194
|
+
$KCODE = "NONE" unless RUBY_VERSION >= "1.9"
|
189
195
|
end
|
190
196
|
|
191
197
|
it_should_behave_like "class with generated accessor methods added"
|
@@ -197,7 +203,7 @@ describe Morph do
|
|
197
203
|
|
198
204
|
describe "when morph method used to set japanese and latin unicode attribute name with a value" do
|
199
205
|
before :each do
|
200
|
-
$KCODE = "u"
|
206
|
+
$KCODE = "u" unless RUBY_VERSION >= "1.9"
|
201
207
|
remove_morph_methods
|
202
208
|
@age = 19
|
203
209
|
@attribute = "ページビュー_graph"
|
@@ -206,7 +212,7 @@ describe Morph do
|
|
206
212
|
end
|
207
213
|
|
208
214
|
after :all do
|
209
|
-
$KCODE = "NONE"
|
215
|
+
$KCODE = "NONE" unless RUBY_VERSION >= "1.9"
|
210
216
|
end
|
211
217
|
|
212
218
|
it_should_behave_like "class with generated accessor methods added"
|
@@ -434,14 +440,22 @@ describe Morph do
|
|
434
440
|
company_details = Morph.from_hash(h, Company::House)
|
435
441
|
company_details.class.name.should == 'Company::House::CompanyDetails'
|
436
442
|
morph_methods = company_details.class.morph_methods
|
437
|
-
|
438
|
-
|
443
|
+
if RUBY_VERSION >= "1.9"
|
444
|
+
morph_methods.include?(:last_full_mem_date).should be_true
|
445
|
+
morph_methods.include?(:accounts).should be_true
|
446
|
+
morph_methods.delete(:accounts)
|
447
|
+
morph_methods.include?(:accounts).should be_false
|
448
|
+
morph_methods = company_details.class.morph_methods
|
449
|
+
morph_methods.include?(:accounts).should be_true
|
450
|
+
else
|
451
|
+
morph_methods.include?('last_full_mem_date').should be_true
|
452
|
+
morph_methods.include?('accounts').should be_true
|
453
|
+
morph_methods.delete('accounts')
|
454
|
+
morph_methods.include?('accounts').should be_false
|
455
|
+
morph_methods = company_details.class.morph_methods
|
456
|
+
morph_methods.include?('accounts').should be_true
|
457
|
+
end
|
439
458
|
|
440
|
-
morph_methods.delete('accounts')
|
441
|
-
morph_methods.include?('accounts').should be_false
|
442
|
-
morph_methods = company_details.class.morph_methods
|
443
|
-
morph_methods.include?('accounts').should be_true
|
444
|
-
|
445
459
|
company_details.accounts.class.name.should == 'Company::House::Accounts'
|
446
460
|
company_details.accounts.overdue.should == 'NO'
|
447
461
|
company_details.last_full_mem_date.should == "2002-03-25"
|
@@ -500,9 +514,9 @@ xmlns_xsi: http://www.w3.org/2001/XMLSchema-instance
|
|
500
514
|
xsi_schema_location: xmlgwdev.companieshouse.gov.uk/v1-0/schema/CompanyDetails.xsd|
|
501
515
|
end
|
502
516
|
end
|
503
|
-
|
517
|
+
|
504
518
|
describe 'creating from xml' do
|
505
|
-
|
519
|
+
|
506
520
|
def check_councils councils, class_name
|
507
521
|
councils.class.should == Array
|
508
522
|
councils.size.should == 2
|
@@ -515,7 +529,7 @@ xsi_schema_location: xmlgwdev.companieshouse.gov.uk/v1-0/schema/CompanyDetails.x
|
|
515
529
|
councils = Morph.from_xml(xml)
|
516
530
|
check_councils councils, 'Morph::Council'
|
517
531
|
end
|
518
|
-
|
532
|
+
|
519
533
|
describe 'when module name is supplied' do
|
520
534
|
it 'should create classes and object instances' do
|
521
535
|
Object.const_set 'Ppc', Module.new
|
@@ -537,8 +551,8 @@ xsi_schema_location: xmlgwdev.companieshouse.gov.uk/v1-0/schema/CompanyDetails.x
|
|
537
551
|
end
|
538
552
|
end
|
539
553
|
|
540
|
-
describe 'creating from' do
|
541
|
-
|
554
|
+
describe 'creating from' do
|
555
|
+
|
542
556
|
def check_councillors councillors, class_name, nil_value=''
|
543
557
|
councillors.class.should == Array
|
544
558
|
councillors.size.should == 2
|
@@ -565,7 +579,7 @@ xsi_schema_location: xmlgwdev.companieshouse.gov.uk/v1-0/schema/CompanyDetails.x
|
|
565
579
|
check_councillors councillors, 'Morph::Councillor'
|
566
580
|
end
|
567
581
|
end
|
568
|
-
|
582
|
+
|
569
583
|
describe 'when class name and module name is supplied' do
|
570
584
|
it 'should create classes and object instances' do
|
571
585
|
Object.const_set 'Ppc', Module.new
|
@@ -576,20 +590,20 @@ xsi_schema_location: xmlgwdev.companieshouse.gov.uk/v1-0/schema/CompanyDetails.x
|
|
576
590
|
|
577
591
|
def tsv
|
578
592
|
%Q[name party councillors councils council_experience
|
579
|
-
Ted Roe labour Councillor for Stretford Ward Trafford Council
|
580
|
-
Ali Davidson labour Basildon District Council
|
593
|
+
Ted Roe labour Councillor for Stretford Ward Trafford Council
|
594
|
+
Ali Davidson labour Basildon District Council
|
581
595
|
]
|
582
596
|
end
|
583
597
|
end
|
584
598
|
|
585
|
-
describe 'csv (comma separated value)' do
|
599
|
+
describe 'csv (comma separated value)' do
|
586
600
|
describe 'when class name is supplied' do
|
587
601
|
it 'should create classes and object instances' do
|
588
602
|
councillors = Morph.from_csv(csv, 'Councillor')
|
589
603
|
check_councillors councillors, 'Morph::Councillor', nil
|
590
604
|
end
|
591
605
|
end
|
592
|
-
|
606
|
+
|
593
607
|
describe 'when class name and module name is supplied' do
|
594
608
|
it 'should create classes and object instances' do
|
595
609
|
Object.const_set 'Ppc', Module.new
|
@@ -597,7 +611,7 @@ Ali Davidson labour Basildon District Council
|
|
597
611
|
check_councillors councillors, 'Ppc::Councillor', nil
|
598
612
|
end
|
599
613
|
end
|
600
|
-
|
614
|
+
|
601
615
|
def csv
|
602
616
|
%Q[name,party,councillors,councils,council_experience
|
603
617
|
Ted Roe,labour,Councillor for Stretford Ward,Trafford Council,
|
data/spec/morph_spec_helper.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require File.dirname(__FILE__) + '/../lib/morph'
|
2
3
|
|
3
4
|
module MorphSpecHelperMethods
|
@@ -68,19 +69,35 @@ describe "class with generated accessor methods added", :shared => true do
|
|
68
69
|
after :all do remove_morph_methods; end
|
69
70
|
|
70
71
|
it 'should add reader method to class instance_methods list' do
|
71
|
-
|
72
|
+
if RUBY_VERSION >= "1.9"
|
73
|
+
each_attribute { |a| instance_methods.should include(a.to_s.to_sym) }
|
74
|
+
else
|
75
|
+
each_attribute { |a| instance_methods.should include(a.to_s) }
|
76
|
+
end
|
72
77
|
end
|
73
78
|
|
74
79
|
it 'should add writer method to class instance_methods list' do
|
75
|
-
|
80
|
+
if RUBY_VERSION >= "1.9"
|
81
|
+
each_attribute { |a| instance_methods.should include("#{a}=".to_sym) }
|
82
|
+
else
|
83
|
+
each_attribute { |a| instance_methods.should include("#{a}=") }
|
84
|
+
end
|
76
85
|
end
|
77
86
|
|
78
87
|
it 'should add reader method to class morph_methods list' do
|
79
|
-
|
88
|
+
if RUBY_VERSION >= "1.9"
|
89
|
+
each_attribute { |a| morph_methods.should include(a.to_s.to_sym) }
|
90
|
+
else
|
91
|
+
each_attribute { |a| morph_methods.should include(a.to_s) }
|
92
|
+
end
|
80
93
|
end
|
81
94
|
|
82
95
|
it 'should add writer method to class morph_methods list' do
|
83
|
-
|
96
|
+
if RUBY_VERSION >= "1.9"
|
97
|
+
each_attribute { |a| morph_methods.should include("#{a}=".to_sym) }
|
98
|
+
else
|
99
|
+
each_attribute { |a| morph_methods.should include("#{a}=") }
|
100
|
+
end
|
84
101
|
end
|
85
102
|
|
86
103
|
it 'should only have generated accessor methods in morph_methods list' do
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: morph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Rob McKinnon
|
@@ -14,21 +15,23 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-10-18 00:00:00 +01:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
22
|
+
name: activesupport
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 11
|
27
30
|
segments:
|
28
|
-
-
|
29
|
-
- 5
|
31
|
+
- 2
|
30
32
|
- 0
|
31
|
-
|
33
|
+
- 2
|
34
|
+
version: 2.0.2
|
32
35
|
type: :runtime
|
33
36
|
version_requirements: *id001
|
34
37
|
description: |
|
@@ -73,16 +76,20 @@ rdoc_options:
|
|
73
76
|
require_paths:
|
74
77
|
- lib
|
75
78
|
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
76
80
|
requirements:
|
77
81
|
- - ">="
|
78
82
|
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
79
84
|
segments:
|
80
85
|
- 0
|
81
86
|
version: "0"
|
82
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
83
89
|
requirements:
|
84
90
|
- - ">="
|
85
91
|
- !ruby/object:Gem::Version
|
92
|
+
hash: 11
|
86
93
|
segments:
|
87
94
|
- 1
|
88
95
|
- 2
|
@@ -90,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
97
|
requirements: []
|
91
98
|
|
92
99
|
rubyforge_project: morph
|
93
|
-
rubygems_version: 1.3.
|
100
|
+
rubygems_version: 1.3.7
|
94
101
|
signing_key:
|
95
102
|
specification_version: 3
|
96
103
|
summary: Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.
|