valuable 0.8 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -65,6 +65,22 @@ _light weight type casting_
65
65
  >> joe.average
66
66
  => 0.25
67
67
 
68
+ _aliases_
69
+
70
+ # This example requires active_support because of Hash.from_xml
71
+
72
+ class Software < Valuable
73
+ has_value :name, :alias => 'Title'
74
+ end
75
+
76
+ >> xml = '<software><Title>Windows XP</Title></software>'
77
+
78
+ >> xp = Software.new(:Title => Hash.from_xml(xml)['software'])
79
+
80
+ >> xp.name
81
+ => "Windows XP"
82
+
83
+
68
84
  _I find myself using classes to format things... ( PhoneNumber is provided in `/examples` )_
69
85
 
70
86
  class School < Valuable
@@ -7,15 +7,16 @@
7
7
  #
8
8
  # has_value :number, :klass => :integer
9
9
  # has_value :color, :default => 'yellow'
10
- # has_collection :riders
10
+ # has_collection :riders, :alias => 'Passengers'
11
11
  #
12
12
  # end
13
13
  #
14
14
  # >> Bus.attributes
15
15
  # => [:number, :color, :riders]
16
- # >> bus = Bus.new(:number => '3', :riders => ['GOF', 'Fowler', 'Mort']
16
+ # >> bus = Bus.new(:number => '3', :Passengers => ['GOF', 'Fowler', 'Mort']
17
17
  # >> bus.attributes
18
18
  # => {:number => 3, :riders => ['GOF', 'Fowler', 'Mort'], :color => 'yellow'}
19
+ #
19
20
  class Valuable
20
21
 
21
22
  # Returns a Hash representing all known values. Values are set three ways:
@@ -23,8 +24,9 @@ class Valuable
23
24
  # (1) a default value
24
25
  # (2) they were passed to the constructor
25
26
  # Bus.new(:color => 'green')
26
- # (3) they were set via their namesake setter
27
+ # (3) they were set via their namesake setter or alias setter
27
28
  # bus.color = 'green'
29
+ # bus.Passengers = ['bill', 'steve']
28
30
  #
29
31
  # Values that have not been set and have no default not appear in this
30
32
  # collection. Their namesake attribute methods will respond with nil.
@@ -74,6 +76,10 @@ class Valuable
74
76
  # :klass - light weight type casting. Use :integer, :string or
75
77
  # :boolean. Alternately, supply a class.
76
78
  #
79
+ # :alias - creates a second setter with the specified name. This
80
+ # allows you to accept information from some other party using their
81
+ # vocabulary. Alias does not currently create a getter.
82
+ #
77
83
  # When a :klassified attribute is set to some new value, if the value
78
84
  # is not nil and is not already of that class, the value will be cast
79
85
  # to the specified klass. In the case of :integer, it wil be done via
@@ -101,7 +107,8 @@ class Valuable
101
107
  create_question_for(name) if options[:klass] == :boolean
102
108
  create_negative_question_for(name, options[:negative]) if options[:klass] == :boolean && options[:negative]
103
109
 
104
- create_setter_for(name, options[:klass], options[:default])
110
+ create_setter_for(name, name, options[:klass])
111
+ create_setter_for(name, options[:alias], options[:klass]) if options[:alias]
105
112
 
106
113
  check_options_validity(name, options)
107
114
  end
@@ -110,44 +117,44 @@ class Valuable
110
117
  # is called both by the constructor. The constructor handles type
111
118
  # casting. Setting values via the attributes hash avoids the method
112
119
  # defined here.
113
- def create_setter_for(name, klass, default)
120
+ def create_setter_for(attribute, method_name, klass)
114
121
 
115
122
  case klass
116
123
  when NilClass
117
124
 
118
- define_method "#{name}=" do |value|
119
- attributes[name] = value
125
+ define_method "#{method_name}=" do |value|
126
+ attributes[attribute] = value
120
127
  end
121
128
 
122
129
  when :integer
123
130
 
124
- define_method "#{name}=" do |value|
131
+ define_method "#{method_name}=" do |value|
125
132
  value_as_integer = value && value.to_i
126
- attributes[name] = value_as_integer
133
+ attributes[attribute] = value_as_integer
127
134
  end
128
135
 
129
136
  when :string
130
137
 
131
- define_method "#{name}=" do |value|
138
+ define_method "#{method_name}=" do |value|
132
139
  value_as_string = value && value.to_s
133
- attributes[name] = value_as_string
140
+ attributes[attribute] = value_as_string
134
141
  end
135
142
 
136
143
  when :boolean
137
144
 
138
- define_method "#{name}=" do |value|
139
- attributes[name] = value == '0' ? false : !!value
145
+ define_method "#{method_name}=" do |value|
146
+ attributes[attribute] = value == '0' ? false : !!value
140
147
  end
141
148
 
142
149
  else
143
150
 
144
- define_method "#{name}=" do |value|
151
+ define_method "#{method_name}=" do |value|
145
152
  if value.nil?
146
- attributes[name] = nil
153
+ attributes[attribute] = nil
147
154
  elsif value.is_a? klass
148
- attributes[name] = value
155
+ attributes[attribute] = value
149
156
  else
150
- attributes[name] = klass.new(value)
157
+ attributes[attribute] = klass.new(value)
151
158
  end
152
159
  end
153
160
  end
@@ -216,7 +223,7 @@ class Valuable
216
223
  end
217
224
 
218
225
  def known_options
219
- [:klass, :default, :negative]
226
+ [:klass, :default, :negative, :alias]
220
227
  end
221
228
 
222
229
  # this helper raises an exception if the options passed to has_value
@@ -58,7 +58,7 @@ spec = Gem::Specification.new do |s|
58
58
  s.rubyforge_project = RUBY_FORGE_PROJECT
59
59
  s.author = 'Johnathon Wright'
60
60
  s.email = 'jw@mustmodify.com'
61
- s.homepage = 'http://www.github.com/mustmodify/valuable'
61
+ s.homepage = 'http://valuable.mustmodify.com'
62
62
  end
63
63
 
64
64
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -0,0 +1,22 @@
1
+ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'test/unit'
4
+ require 'valuable.rb'
5
+
6
+ class Software < Valuable
7
+ has_value :name, :alias => :title
8
+ has_value :enterprise_namespace, :alias => 'EnterpriseNamespace'
9
+ end
10
+
11
+ class AliasTest < Test::Unit::TestCase
12
+
13
+ def test_that_values_can_be_set_using_their_alias
14
+ software = Software.new(:title => 'PostIt')
15
+ assert_equal 'PostIt', software.name
16
+ end
17
+
18
+ def test_that_aliases_can_be_strings
19
+ software = Software.new('EnterpriseNamespace' => 'Enterprisey')
20
+ assert_equal 'Enterprisey', software.enterprise_namespace
21
+ end
22
+ end
@@ -1,5 +1,6 @@
1
1
  $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
2
2
 
3
+ require 'rubygems'
3
4
  require 'test/unit'
4
5
  require 'valuable.rb'
5
6
  require 'mocha'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valuable
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.8"
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johnathon Wright
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-16 00:00:00 -06:00
12
+ date: 2010-04-06 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,7 +26,7 @@ files:
26
26
  - README.markdown
27
27
  - rakefile.rb
28
28
  has_rdoc: true
29
- homepage: http://www.github.com/mustmodify/valuable
29
+ homepage: http://valuable.mustmodify.com
30
30
  licenses: []
31
31
 
32
32
  post_install_message:
@@ -54,7 +54,8 @@ signing_key:
54
54
  specification_version: 3
55
55
  summary: attr_accessor on steroids with defaults, constructor, and light casting.
56
56
  test_files:
57
- - test/bad_attributes_test.rb
58
- - test/deprecated_test.rb
59
57
  - test/inheritance_test.rb
60
58
  - test/valuable_test.rb
59
+ - test/bad_attributes_test.rb
60
+ - test/deprecated_test.rb
61
+ - test/alias_test.rb