charisma 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -147,6 +147,10 @@ irb(main):001:0> Foo.new(:size => 10).characteristics[:size].feet
147
147
  => 32.808399000000003
148
148
  ```
149
149
 
150
+ ## JSON
151
+
152
+ Charisma now provides `#as_json` for characteristics---convert to actual JSON with your favorite JSON library's `#to_json`.
153
+
150
154
  ## Copyright
151
155
 
152
156
  Copyright (c) 2011 Andy Rossmeissl. See LICENSE for details.
data/Rakefile CHANGED
@@ -18,6 +18,11 @@ require 'rake'
18
18
  require 'bueller'
19
19
  Bueller::Tasks.new
20
20
 
21
+ task :start_coverage do
22
+ SimpleCov.start
23
+ end
24
+ task :coverage => [:start_coverage, :test]
25
+
21
26
  require 'rake/testtask'
22
27
  Rake::TestTask.new(:test) do |test|
23
28
  test.libs << 'lib' << 'test'
@@ -25,13 +30,6 @@ Rake::TestTask.new(:test) do |test|
25
30
  test.verbose = true
26
31
  end
27
32
 
28
- require 'rcov/rcovtask'
29
- Rcov::RcovTask.new do |test|
30
- test.libs << 'test'
31
- test.pattern = 'test/**/test_*.rb'
32
- test.verbose = true
33
- end
34
-
35
33
  task :default => :test
36
34
 
37
35
  require 'rake/rdoctask'
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
32
32
  s.add_development_dependency 'bundler'
33
33
  s.add_development_dependency 'bueller'
34
34
  s.add_development_dependency 'rake'
35
- s.add_development_dependency 'rcov'
35
+ s.add_development_dependency 'simplecov'
36
36
  s.add_development_dependency 'supermodel'
37
37
  end
38
38
 
@@ -23,17 +23,17 @@ module Charisma
23
23
  end
24
24
  end
25
25
 
26
- # The hashed wrapped by the curator that actually stores the computed characteristics'
26
+ # The special hash wrapped by the curator that actually stores the computed characteristics.
27
27
  def characteristics
28
- return @characteristics unless @characteristics.nil?
28
+ return @characteristics if @characteristics
29
29
 
30
- @characteristics = ::Hash.new do |h, key|
30
+ hsh = Hash.new do |_, key|
31
31
  if characterization = subject.class.characterization[key]
32
32
  Curation.new nil, characterization
33
33
  end
34
34
  end
35
- @characteristics.extend LooseEquality
36
- @characteristics
35
+ hsh.extend LooseEquality
36
+ @characteristics = hsh
37
37
  end
38
38
 
39
39
  # Store a late-defined characteristic, with a Charisma wrapper.
@@ -51,13 +51,13 @@ module Charisma
51
51
  # (see #inpsect)
52
52
  def to_s; inspect end
53
53
 
54
- # Provide a hash of display-friendly presentations of the computed characteristics' values.
54
+ # Provide a hash of the plain values (dropping presentation information).
55
55
  #
56
- # This is just a convenience method for a common use case, namely injecting a hash with the display-friendly presentations.
56
+ # Previous versions of leap returned a hash of display-friendly representations; this was rather surprising and not especially useful.
57
57
  # @return [Hash]
58
58
  def to_hash
59
59
  characteristics.inject({}) do |memo, (k, v)|
60
- memo[k] = v.to_s
60
+ memo[k] = v.value
61
61
  memo
62
62
  end
63
63
  end
@@ -26,7 +26,7 @@ module Charisma
26
26
  self.value = value
27
27
  end
28
28
 
29
- def_delegators :render, :to_s
29
+ def_delegators :render_string, :to_s
30
30
 
31
31
  def ==(other)
32
32
  a = self.value
@@ -57,18 +57,29 @@ module Charisma
57
57
  characteristic.try(:measurement_class).try(:unit_abbreviation)
58
58
  end
59
59
 
60
+ # Render a JSON-like object for later conversion to JSON
61
+ def as_json(*)
62
+ if characteristic.measurement
63
+ characteristic.measurement_class.new(value).as_json
64
+ elsif value.respond_to? :as_json
65
+ value.as_json
66
+ else
67
+ value
68
+ end
69
+ end
70
+
60
71
  private
61
72
 
62
73
  # If this curation deals with a measured characteristic, this method will delegate appropriate unit-name methods like <tt>#kilograms</tt> to <tt>#render</tt>.
63
74
  def establish_units_methods
64
75
  if characteristic and characteristic.measurement and conversions = Conversions.conversions[units.to_sym]
65
- self.class.def_delegators :render, *conversions.keys
76
+ self.class.def_delegators :render_string, *conversions.keys
66
77
  end
67
78
  end
68
79
 
69
80
  # Provide a display-friendly presentation of the computed characteristic's value.
70
81
  # @return [String]
71
- def render
82
+ def render_string
72
83
  return value unless characteristic
73
84
  if characteristic.proc
74
85
  render_proc
@@ -52,6 +52,16 @@ module Charisma
52
52
  super
53
53
  end
54
54
  end
55
+
56
+ # Provide a hash form
57
+ def to_hash
58
+ { :value => value, :units => units.to_s }
59
+ end
60
+
61
+ # Provide a hash for later conversion to JSON
62
+ def as_json
63
+ to_hash
64
+ end
55
65
 
56
66
  class << self
57
67
  # Define the units used with this measurement.
@@ -1,4 +1,4 @@
1
1
  module Charisma
2
2
  # Charisma's version
3
- VERSION = "0.3.3"
3
+ VERSION = "0.3.4"
4
4
  end
@@ -17,4 +17,8 @@ class Planet < SuperModel::Base
17
17
  raise "unknown"
18
18
  end
19
19
  end
20
+
21
+ def as_json
22
+ { :name => name }
23
+ end
20
24
  end
@@ -127,7 +127,7 @@ class TestCharisma < Test::Unit::TestCase
127
127
 
128
128
  def test_020_to_hash
129
129
  spaceship = Spaceship.new :name => 'Amaroq', :window_count => 2, :size => 10
130
- assert_equal({:name => 'Amaroq', :window_count => '2 windows', :size => '10 m'}, spaceship.characteristics.to_hash)
130
+ assert_equal({:name => 'Amaroq', :window_count => 2, :size => 10}, spaceship.characteristics.to_hash)
131
131
  end
132
132
 
133
133
  def test_021_dup
@@ -144,4 +144,12 @@ class TestCharisma < Test::Unit::TestCase
144
144
  assert_equal nil, spaceship.characteristics[:name].units
145
145
  assert_equal :hollagrams, spaceship.characteristics[:weight].units
146
146
  end
147
+
148
+ def test_023_json
149
+ planet = Planet.create :name => 'Pluto'
150
+ spaceship = Spaceship.new :weight => 100, :window_count => 3, :destination => planet
151
+ assert_equal({:value => 100, :units => 'hollagrams'}, spaceship.characteristics[:weight].as_json)
152
+ assert_equal 3, spaceship.characteristics[:window_count].as_json
153
+ assert_equal({:name => 'Pluto'}, spaceship.characteristics[:destination].as_json)
154
+ end
147
155
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: charisma
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-09-29 00:00:00.000000000Z
13
+ date: 2011-09-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
17
- requirement: &2158256360 !ruby/object:Gem::Requirement
17
+ requirement: &2164435900 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2158256360
25
+ version_requirements: *2164435900
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: blockenspiel
28
- requirement: &2158255680 !ruby/object:Gem::Requirement
28
+ requirement: &2164435100 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2158255680
36
+ version_requirements: *2164435100
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: conversions
39
- requirement: &2158250620 !ruby/object:Gem::Requirement
39
+ requirement: &2164434200 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *2158250620
47
+ version_requirements: *2164434200
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &2158250040 !ruby/object:Gem::Requirement
50
+ requirement: &2164432560 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2158250040
58
+ version_requirements: *2164432560
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: bueller
61
- requirement: &2158249400 !ruby/object:Gem::Requirement
61
+ requirement: &2164385780 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2158249400
69
+ version_requirements: *2164385780
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rake
72
- requirement: &2158248760 !ruby/object:Gem::Requirement
72
+ requirement: &2164382980 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2158248760
80
+ version_requirements: *2164382980
81
81
  - !ruby/object:Gem::Dependency
82
- name: rcov
83
- requirement: &2158248140 !ruby/object:Gem::Requirement
82
+ name: simplecov
83
+ requirement: &2164381740 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2158248140
91
+ version_requirements: *2164381740
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: supermodel
94
- requirement: &2158247460 !ruby/object:Gem::Requirement
94
+ requirement: &2164379660 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,7 +99,7 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *2158247460
102
+ version_requirements: *2164379660
103
103
  description: Define strategies for accessing and displaying a subset of your classes'
104
104
  attributes
105
105
  email: andy@rossmeissl.net
@@ -151,9 +151,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
151
  - - ! '>='
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0'
154
- segments:
155
- - 0
156
- hash: -4326195820228354583
157
154
  required_rubygems_version: !ruby/object:Gem::Requirement
158
155
  none: false
159
156
  requirements:
@@ -162,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
159
  version: 1.3.7
163
160
  requirements: []
164
161
  rubyforge_project:
165
- rubygems_version: 1.8.5
162
+ rubygems_version: 1.8.15
166
163
  signing_key:
167
164
  specification_version: 3
168
165
  summary: Curate your rich Ruby objects' attributes
@@ -176,3 +173,4 @@ test_files:
176
173
  - test/support/spaceship_fuel.rb
177
174
  - test/support/spaceship_make.rb
178
175
  - test/test_charisma.rb
176
+ has_rdoc: