morph 0.1.4 → 0.1.5

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.1.5. added morph_attributes instance method
2
+
1
3
  v0.1.4. can now pass code block to customize the dynamically added methods
2
4
 
3
5
  v0.1.3. can now set hash of attributes via morph method
data/README CHANGED
@@ -82,7 +82,7 @@ class VegieFoxes
82
82
  include Morph
83
83
 
84
84
  def method_missing symbol, *args
85
- if (is_writer = symbol.to_s =~ /=\Z/)
85
+ if (is_writer = symbol.to_s =~ /=$/)
86
86
  morph_method_missing(symbol, *args) do |base, attribute|
87
87
  create_accessors base, attribute
88
88
  end
data/examples/hubbit.rb CHANGED
@@ -1,4 +1,6 @@
1
- require 'rubygems'; require 'hpricot'; require 'open-uri'; require 'morph'
1
+ require 'morph'
2
+
3
+ require 'rubygems'; require 'hpricot'; require 'open-uri'
2
4
 
3
5
  # An example of Morph playing with Hpricot
4
6
  class Hubbit
data/lib/morph.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Morph
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
 
4
4
  def self.included(base)
5
5
  base.extend ClassMethods
@@ -24,6 +24,15 @@ module Morph
24
24
  class_eval { define_method name, &block }
25
25
  end
26
26
 
27
+ def show_ruby
28
+ begin
29
+ require 'ruby2ruby'
30
+ puts Ruby2Ruby.translate(self)
31
+ rescue LoadError
32
+ puts "You need to install the ruby2ruby gem before calling show_ruby"
33
+ end
34
+ end
35
+
27
36
  protected
28
37
 
29
38
  def method_added symbol
@@ -38,13 +47,8 @@ module Morph
38
47
 
39
48
  module MethodMissing
40
49
  def method_missing symbol, *args
41
- is_writer = symbol.to_s =~ /=\Z/
42
-
43
- if is_writer
44
- morph_method_missing symbol, *args
45
- else
46
- super
47
- end
50
+ is_writer = symbol.to_s =~ /=$/
51
+ is_writer ? morph_method_missing(symbol, *args) : super
48
52
  end
49
53
  end
50
54
 
@@ -69,16 +73,25 @@ module Morph
69
73
  # p order # -> #<Order:0x33c50c @lemon=false, @milk="yes please",
70
74
  # @payment_type="will wash dishes", @sugars=2, @drink="tea">
71
75
  #
72
- def morph attributes, value=nil
73
- if attributes.is_a? Hash
74
- attributes.each { |a, v| morph(a, v) }
76
+ def morph attributes_or_label, value=nil
77
+ if attributes_or_label.is_a? Hash
78
+ attributes_or_label.each { |a, v| morph(a, v) }
75
79
  else
76
- label = attributes
77
- attribute = label.is_a?(String) ? convert_to_morph_method_name(label) : label
80
+ attribute = convert_to_morph_method_name(attributes_or_label)
78
81
  send("#{attribute}=".to_sym, value)
79
82
  end
80
83
  end
81
84
 
85
+ def morph_attributes
86
+ attributes = self.class.morph_methods.inject({}) do |hash, attribute|
87
+ unless attribute =~ /=\Z/
88
+ symbol = attribute.to_sym
89
+ hash[symbol] = send(symbol)
90
+ end
91
+ hash
92
+ end
93
+ end
94
+
82
95
  protected
83
96
 
84
97
  def morph_method_missing symbol, *args
@@ -88,7 +101,7 @@ module Morph
88
101
  raise "'#{attribute}' is an instance_method on Object, cannot create accessor methods for '#{attribute}'"
89
102
  elsif argument_provided? args
90
103
  base = self.class
91
- base.adding_morph_method= true
104
+ base.adding_morph_method = true
92
105
 
93
106
  if block_given?
94
107
  yield base, attribute
@@ -96,7 +109,7 @@ module Morph
96
109
  base.class_eval "attr_accessor :#{attribute}"
97
110
  send(symbol, *args)
98
111
  end
99
- base.adding_morph_method= false
112
+ base.adding_morph_method = false
100
113
  end
101
114
  end
102
115
 
@@ -107,7 +120,7 @@ module Morph
107
120
  end
108
121
 
109
122
  def convert_to_morph_method_name label
110
- name = label.downcase.tr('()*',' ').gsub('%','percentage').strip.chomp(':').strip.gsub(/\s/,'_').squeeze('_')
123
+ name = label.to_s.downcase.tr('()*',' ').gsub('%','percentage').strip.chomp(':').strip.gsub(/\s/,'_').squeeze('_')
111
124
  name = '_'+name if name =~ /^\d/
112
125
  name
113
126
  end
data/morph.gemspec CHANGED
@@ -1,16 +1,16 @@
1
1
 
2
- # Gem::Specification for Morph-0.1.4
2
+ # Gem::Specification for Morph-0.1.5
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.4"
7
+ s.version = "0.1.5"
8
8
 
9
9
  s.specification_version = 2 if s.respond_to? :specification_version=
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.authors = ["Rob McKinnon"]
13
- s.date = %q{2008-04-10}
13
+ s.date = %q{2008-04-14}
14
14
  s.description = %q{Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.}
15
15
  s.email = ["rob ~@nospam@~ rubyforge.org"]
16
16
  s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README"]
data/spec/morph_spec.rb CHANGED
@@ -15,6 +15,10 @@ describe Morph, "when writer method that didn't exist before is called with non-
15
15
  it 'should return assigned value when reader method called' do
16
16
  @morph.noise.should == @quack
17
17
  end
18
+
19
+ it 'should return hash of attributes when morph_attributes called' do
20
+ @morph.morph_attributes.should == {@attribute.to_sym => @quack}
21
+ end
18
22
  end
19
23
 
20
24
  describe Morph, "when writer method that didn't exist before is called with nil value" do
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.4
4
+ version: 0.1.5
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-04-10 00:00:00 +01:00
12
+ date: 2008-04-14 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15