morph 0.1.2 → 0.1.3

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.1.3. can now set hash of attributes via morph method
2
+
1
3
  v0.1.2. fixed problems in packaging of v0.1.1 gem
2
4
 
3
5
  v0.1.1. calling unknown reader method no longer adds accessor methods to class
data/README CHANGED
@@ -60,16 +60,29 @@ So, a new company method has appeared:
60
60
  dhh.company #=> "37signals"
61
61
 
62
62
 
63
+ How about adding a hash of attribute values?
64
+
65
+ why.morph :drink => 'tea', :spoons_of_sugar => 2, :milk => 'prefer soya thanks'
66
+
67
+ why.drink # => "tea"
68
+ why.spoons_of_sugar # => 2
69
+ why.milk # => "prefer soya thanks"
70
+
71
+
63
72
  Time to print the nascent attribute definitions:
64
73
 
65
74
  puts Hubbit.print_morph_methods
66
75
 
67
- # attr_accessor :name
68
- # attr_accessor :email
69
- # attr_accessor :blog
70
- # attr_accessor :company
71
- # attr_accessor :location
72
- # attr_accessor :projects
76
+ attr_accessor :blog
77
+ attr_accessor :company
78
+ attr_accessor :drink
79
+ attr_accessor :email
80
+ attr_accessor :location
81
+ attr_accessor :milk
82
+ attr_accessor :name
83
+ attr_accessor :projects
84
+ attr_accessor :spoons_of_sugar
85
+ => nil
73
86
 
74
87
 
75
88
  See examples/ directory for more example code.
data/examples/forger.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'hpricot'; require 'open-uri'; require 'morph'
1
+ require 'rubygems'; require 'hpricot'; require 'open-uri'; require 'morph'
2
2
 
3
3
  # An example of Morph playing with Hpricot
4
4
  class Forger
data/examples/hubbit.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'hpricot'; require 'open-uri'; require 'morph'
1
+ require 'rubygems'; require 'hpricot'; require 'open-uri'; require 'morph'
2
2
 
3
3
  # An example of Morph playing with Hpricot
4
4
  class Hubbit
@@ -27,4 +27,3 @@ end
27
27
 
28
28
  # why = Hubbit 'why'
29
29
  # dhh = Hubbit 'dhh'
30
-
data/lib/morph.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Morph
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
 
4
4
  def self.included(base)
5
5
  base.extend ClassMethods
@@ -58,9 +58,33 @@ module Morph
58
58
 
59
59
  module InstanceMethods
60
60
 
61
- def morph label, value
62
- attribute = self.class.convert_to_morph_method_name label
63
- send("#{attribute}=".to_sym, value)
61
+ #
62
+ # Set attribute value(s). Adds accessor methods to class if
63
+ # they are not already present.
64
+ #
65
+ # Can be called with a +string+ and a value, a +symbol+ and a value,
66
+ # or with a +hash+ of attribute to value pairs. For example.
67
+ #
68
+ # require 'rubygems'; require 'morph'
69
+ #
70
+ # class Order; include Morph; end
71
+ #
72
+ # order = Order.new
73
+ # order.morph :drink => 'tea', :sugars => 2, 'milk' => 'yes please'
74
+ # order.morph 'Payment type:', 'will wash dishes'
75
+ # order.morph :lemon, false
76
+ #
77
+ # p order # -> #<Order:0x33c50c @lemon=false, @milk="yes please",
78
+ # @payment_type="will wash dishes", @sugars=2, @drink="tea">
79
+ #
80
+ def morph attributes, value=nil
81
+ if attributes.is_a? Hash
82
+ attributes.each { |a, v| morph(a, v) }
83
+ else
84
+ label = attributes
85
+ attribute = label.is_a?(String) ? self.class.convert_to_morph_method_name(label) : label
86
+ send("#{attribute}=".to_sym, value)
87
+ end
64
88
  end
65
89
 
66
90
  def method_missing symbol, *args
data/morph.gemspec CHANGED
@@ -1,22 +1,23 @@
1
1
 
2
- # Gem::Specification for Morph-0.1.2
2
+ # Gem::Specification for Morph-0.1.3
3
3
  # Originally generated by Echoe
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = %q{morph}
7
- s.version = "0.1.2"
7
+ s.version = "0.1.3"
8
8
 
9
9
  s.specification_version = 2 if s.respond_to? :specification_version=
10
10
 
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
12
  s.authors = ["Rob McKinnon"]
12
- s.date = %q{2008-03-25}
13
+ s.date = %q{2008-04-02}
13
14
  s.description = %q{Morph allows you to emerge class definitions via calling assignment methods.}
14
15
  s.email = ["rob ~@nospam@~ rubyforge.org"]
16
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README"]
15
17
  s.files = ["CHANGELOG", "examples/forger.rb", "examples/hubbit.rb", "lib/morph.rb", "LICENSE", "README", "spec/morph_spec.rb", "spec/spec.opts", "Manifest", "morph.gemspec"]
16
18
  s.has_rdoc = true
17
- s.rdoc_options += ['--quiet', '--inline-source', '--main', 'README']
18
- s.extra_rdoc_files = ["README", "CHANGELOG", "LICENSE"]
19
19
  s.homepage = %q{}
20
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Morph", "--main", "README", "--inline-source"]
20
21
  s.require_paths = ["lib"]
21
22
  s.rubyforge_project = %q{morph}
22
23
  s.rubygems_version = %q{1.0.1}
@@ -39,7 +40,7 @@ end
39
40
  # m.description = File.readlines("README").first
40
41
  # m.rubyforge_name = "morph"
41
42
  # m.rdoc_options << '--inline-source'
42
- # m.rdoc_pattern = ["README", "CHANGELOG", "LICENSE", "lib/*"]
43
+ # m.rdoc_pattern = ["README", "CHANGELOG", "LICENSE"]
43
44
  # end
44
45
  #
45
46
  # rescue LoadError
data/spec/morph_spec.rb CHANGED
@@ -56,6 +56,23 @@ describe Morph, 'when morph method used to set attribute value' do
56
56
  end
57
57
  end
58
58
 
59
+ describe Morph, 'when morph method used to set an attribute value hash' do
60
+ before :each do
61
+ remove_morph_methods
62
+ @attributes = [:drink,:sugars,:milk]
63
+ @morph.morph :drink => 'tea', :sugars => 2, :milk => 'yes please'
64
+ @expected_morph_methods_count = 6
65
+ end
66
+
67
+ it_should_behave_like "class with generated accessor methods added"
68
+
69
+ it 'should return assigned value when reader method called' do
70
+ @morph.drink.should == 'tea'
71
+ @morph.sugars.should == 2
72
+ @morph.milk.should == 'yes please'
73
+ end
74
+ end
75
+
59
76
  describe Morph, "when morph method used to set unicode attribute name with a value" do
60
77
  before :each do
61
78
  $KCODE = "u"
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.1.2
4
+ version: 0.1.3
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: 2008-03-25 00:00:00 +00:00
12
+ date: 2008-04-02 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -21,9 +21,9 @@ executables: []
21
21
  extensions: []
22
22
 
23
23
  extra_rdoc_files:
24
- - README
25
24
  - CHANGELOG
26
25
  - LICENSE
26
+ - README
27
27
  files:
28
28
  - CHANGELOG
29
29
  - examples/forger.rb
@@ -39,10 +39,13 @@ has_rdoc: true
39
39
  homepage: ""
40
40
  post_install_message:
41
41
  rdoc_options:
42
- - --quiet
42
+ - --line-numbers
43
43
  - --inline-source
44
+ - --title
45
+ - Morph
44
46
  - --main
45
47
  - README
48
+ - --inline-source
46
49
  require_paths:
47
50
  - lib
48
51
  required_ruby_version: !ruby/object:Gem::Requirement