charisma 0.1.2 → 0.2.0
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/charisma.gemspec +1 -1
- data/lib/charisma.rb +0 -1
- data/lib/charisma/base/class_methods.rb +1 -1
- data/lib/charisma/curator.rb +23 -24
- data/lib/charisma/curator/curation.rb +30 -14
- data/lib/charisma/version.rb +1 -1
- data/test/test_charisma.rb +13 -7
- metadata +4 -4
data/charisma.gemspec
CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
|
|
5
5
|
s.name = 'charisma'
|
6
6
|
s.version = Charisma::VERSION
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
|
-
s.date = "2011-
|
8
|
+
s.date = "2011-05-11"
|
9
9
|
s.authors = ['Andy Rossmeissl', 'Seamus Abshere']
|
10
10
|
s.email = 'andy@rossmeissl.net'
|
11
11
|
s.homepage = 'http://github.com/brighterplanet/charisma'
|
data/lib/charisma.rb
CHANGED
@@ -4,7 +4,6 @@ require 'active_support/version'
|
|
4
4
|
active_support/inflector/methods
|
5
5
|
active_support/core_ext/class/attribute_accessors
|
6
6
|
active_support/core_ext/string/output_safety
|
7
|
-
active_support/core_ext/module/delegation
|
8
7
|
}.each do |active_support_3_requirement|
|
9
8
|
require active_support_3_requirement
|
10
9
|
end if ActiveSupport::VERSION::MAJOR == 3
|
@@ -3,7 +3,7 @@ module Charisma
|
|
3
3
|
module ClassMethods
|
4
4
|
def self.extended(base)
|
5
5
|
base.send :class_variable_set, :@@characterization, Characterization.new
|
6
|
-
base.send :cattr_reader, :characterization
|
6
|
+
base.send :cattr_reader, :characterization, :instance_reader => false
|
7
7
|
end
|
8
8
|
def characterize(&blk)
|
9
9
|
Blockenspiel.invoke(blk, characterization) if block_given?
|
data/lib/charisma/curator.rb
CHANGED
@@ -1,38 +1,37 @@
|
|
1
|
+
require 'forwardable'
|
1
2
|
module Charisma
|
2
3
|
class Curator
|
3
|
-
attr_reader :subject
|
4
|
+
attr_reader :subject, :characteristics
|
4
5
|
|
5
6
|
def initialize(subject)
|
7
|
+
@characteristics = {}.extend LooseEquality
|
6
8
|
@subject = subject
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
Curation.new value, subject.class.characterization[key]
|
9
|
+
subject.class.characterization.keys.each do |key|
|
10
|
+
if value = subject.send(key)
|
11
|
+
self[key] = value
|
12
|
+
end
|
12
13
|
end
|
13
14
|
end
|
14
|
-
|
15
|
-
def
|
16
|
-
subject.class.characterization
|
17
|
-
!!subject.send(key)
|
18
|
-
end
|
15
|
+
|
16
|
+
def []=(key, value)
|
17
|
+
characteristics[key] = Curation.new value, subject.class.characterization[key]
|
19
18
|
end
|
20
|
-
|
21
|
-
def
|
22
|
-
|
23
|
-
if curation = self[key]
|
24
|
-
memo[key] = curation
|
25
|
-
end
|
26
|
-
memo
|
27
|
-
end
|
19
|
+
|
20
|
+
def inspect
|
21
|
+
"<Charisma:Curator #{keys.length} known characteristic(s)>"
|
28
22
|
end
|
29
23
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
24
|
+
def to_s; inspect end
|
25
|
+
|
26
|
+
extend Forwardable
|
27
|
+
delegate [:[], :keys, :slice, :==] => :characteristics
|
28
|
+
|
29
|
+
module LooseEquality
|
30
|
+
def ==(other)
|
31
|
+
return false unless keys.sort == other.keys.sort
|
32
|
+
keys.all? do |k|
|
33
|
+
self[k] == other[k]
|
34
34
|
end
|
35
|
-
memo
|
36
35
|
end
|
37
36
|
end
|
38
37
|
end
|
@@ -1,26 +1,38 @@
|
|
1
|
+
require 'delegate'
|
1
2
|
module Charisma
|
2
3
|
class Curator
|
3
|
-
class Curation
|
4
|
-
|
4
|
+
class Curation < Delegator
|
5
|
+
extend Forwardable
|
6
|
+
attr_accessor :value
|
7
|
+
attr_reader :characteristic
|
5
8
|
|
6
|
-
def initialize(value, characteristic)
|
7
|
-
@value = value
|
9
|
+
def initialize(value, characteristic = nil)
|
8
10
|
@characteristic = characteristic
|
11
|
+
establish_units_methods if characteristic && characteristic.measurement
|
12
|
+
self.value = value
|
9
13
|
end
|
10
14
|
|
11
|
-
delegate :to_s
|
12
|
-
|
13
|
-
def
|
14
|
-
|
15
|
-
render.send(*args)
|
16
|
-
rescue NoMethodError
|
17
|
-
super
|
18
|
-
end
|
15
|
+
delegate [:to_s] => :render
|
16
|
+
|
17
|
+
def inspect
|
18
|
+
"<Charisma::Curator::Curation for :#{characteristic.name} (use #to_s to render value of #{value.class})>"
|
19
19
|
end
|
20
20
|
|
21
|
+
# Delegator methods
|
22
|
+
def __getobj__; value end
|
23
|
+
def __setobj__(obj); self.value = obj end
|
24
|
+
|
21
25
|
private
|
22
26
|
|
27
|
+
def establish_units_methods
|
28
|
+
self.class.delegate [:u, :units] => :render
|
29
|
+
if conversions = Conversions.conversions[units.to_sym]
|
30
|
+
self.class.delegate conversions.keys => :render
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
23
34
|
def render
|
35
|
+
return value unless characteristic
|
24
36
|
if characteristic.proc
|
25
37
|
render_proc
|
26
38
|
elsif characteristic.accessor
|
@@ -43,11 +55,15 @@ module Charisma
|
|
43
55
|
end
|
44
56
|
|
45
57
|
def defer_to_measurement
|
58
|
+
measurement_class.new(value)
|
59
|
+
end
|
60
|
+
|
61
|
+
def measurement_class
|
46
62
|
case characteristic.measurement
|
47
63
|
when Class
|
48
|
-
characteristic.measurement
|
64
|
+
characteristic.measurement
|
49
65
|
when Symbol
|
50
|
-
"Charisma::Measurement::#{characteristic.measurement.to_s.camelize}".constantize
|
66
|
+
"Charisma::Measurement::#{characteristic.measurement.to_s.camelize}".constantize
|
51
67
|
else
|
52
68
|
raise InvalidMeasurementError
|
53
69
|
end
|
data/lib/charisma/version.rb
CHANGED
data/test/test_charisma.rb
CHANGED
@@ -42,14 +42,21 @@ class TestCharisma < Test::Unit::TestCase
|
|
42
42
|
assert_equal '1,000', Charisma::NumberHelper.delimit(1_000)
|
43
43
|
end
|
44
44
|
def test_010_characteristics_slice
|
45
|
-
spaceship = Spaceship.new :name => 'Amaroq'
|
46
|
-
assert_equal '
|
45
|
+
spaceship = Spaceship.new :name => 'Amaroq', :window_count => 2, :size => 10
|
46
|
+
assert_equal '10 m', spaceship.characteristics.slice(:size, :window_count)[:size].to_s
|
47
|
+
assert_equal nil, spaceship.characteristics.slice(:size, :window_count)[:name]
|
48
|
+
assert_equal 2, spaceship.characteristics.slice(:size, :window_count).values.length
|
47
49
|
end
|
48
50
|
def test_011_associated_object_methods
|
49
51
|
planet = Planet.create :name => 'Pluto'
|
50
52
|
spaceship = Spaceship.new :destination => planet
|
51
53
|
assert_equal BigDecimal('1.31e+1_022'), spaceship.characteristics[:destination].mass
|
52
54
|
end
|
55
|
+
def test_011_characteristic_arithmetic
|
56
|
+
amaroq = Spaceship.new :window_count => 8
|
57
|
+
geo = Spaceship.new :window_count => 6
|
58
|
+
assert_equal 2, amaroq.characteristics[:window_count] - geo.characteristics[:window_count]
|
59
|
+
end
|
53
60
|
def test_012_missing_characteristics_come_back_as_nil
|
54
61
|
spaceship = Spaceship.new :name => 'Amaroq'
|
55
62
|
assert_equal nil, spaceship.characteristics[:size]
|
@@ -57,14 +64,13 @@ class TestCharisma < Test::Unit::TestCase
|
|
57
64
|
def test_013_characteristics_keys
|
58
65
|
spaceship = Spaceship.new :name => 'Amaroq'
|
59
66
|
assert_equal [:name].sort_by { |k| k.to_s }, spaceship.characteristics.keys.sort_by { |k| k.to_s }
|
60
|
-
spaceship.weight = 1_000_000
|
61
|
-
assert_equal [:name, :weight].sort_by { |k| k.to_s }, spaceship.characteristics.keys.sort_by { |k| k.to_s }
|
62
67
|
end
|
63
68
|
def test_014_characterization_keys
|
64
69
|
assert_equal [:destination, :fuel, :make, :name, :size, :weight, :window_count].sort_by { |k| k.to_s }, Spaceship.characterization.keys.sort_by { |k| k.to_s }
|
65
70
|
end
|
66
|
-
def
|
67
|
-
|
68
|
-
|
71
|
+
def test_015_characteristic_equality
|
72
|
+
amaroq = Spaceship.new :window_count => 8
|
73
|
+
buster = Spaceship.new :window_count => 8
|
74
|
+
assert_equal amaroq.characteristics, buster.characteristics
|
69
75
|
end
|
70
76
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: charisma
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 1
|
9
8
|
- 2
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andy Rossmeissl
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-05-11 00:00:00 -04:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|