charisma 0.1.0 → 0.1.1
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 +39 -39
- data/lib/charisma.rb +1 -2
- data/lib/charisma/characterization.rb +2 -12
- data/lib/charisma/curator.rb +32 -4
- data/lib/charisma/number_helper.rb +3 -0
- data/lib/charisma/version.rb +3 -3
- data/test/helper.rb +73 -63
- data/test/test_charisma.rb +70 -41
- metadata +4 -4
data/charisma.gemspec
CHANGED
@@ -1,39 +1,39 @@
|
|
1
|
-
$:.push File.expand_path("../lib", __FILE__)
|
2
|
-
require 'charisma/version'
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = 'charisma'
|
6
|
-
s.version = Charisma::VERSION
|
7
|
-
s.platform = Gem::Platform::RUBY
|
8
|
-
s.date = "2011-
|
9
|
-
s.authors = ['Andy Rossmeissl', 'Seamus Abshere']
|
10
|
-
s.email = 'andy@rossmeissl.net'
|
11
|
-
s.homepage = 'http://github.com/brighterplanet/charisma'
|
12
|
-
s.summary = %Q{Curate your rich Ruby objects' attributes}
|
13
|
-
s.description = %Q{Define strategies for accessing and displaying a subset of your classes' attributes}
|
14
|
-
s.extra_rdoc_files = [
|
15
|
-
'LICENSE',
|
16
|
-
'README.rdoc',
|
17
|
-
]
|
18
|
-
|
19
|
-
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.7')
|
20
|
-
s.rubygems_version = '1.3.7'
|
21
|
-
s.specification_version = 3
|
22
|
-
|
23
|
-
s.files = `git ls-files`.split("\n")
|
24
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
-
s.require_paths = ['lib']
|
27
|
-
|
28
|
-
s.add_dependency 'activesupport'
|
29
|
-
s.add_dependency 'actionpack'
|
30
|
-
s.add_dependency 'blockenspiel'
|
31
|
-
s.add_dependency 'conversions'
|
32
|
-
|
33
|
-
s.add_development_dependency 'bundler'
|
34
|
-
s.add_development_dependency 'bueller'
|
35
|
-
s.add_development_dependency 'rake'
|
36
|
-
s.add_development_dependency 'rcov'
|
37
|
-
s.add_development_dependency 'supermodel'
|
38
|
-
end
|
39
|
-
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'charisma/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'charisma'
|
6
|
+
s.version = Charisma::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.date = "2011-04-29"
|
9
|
+
s.authors = ['Andy Rossmeissl', 'Seamus Abshere']
|
10
|
+
s.email = 'andy@rossmeissl.net'
|
11
|
+
s.homepage = 'http://github.com/brighterplanet/charisma'
|
12
|
+
s.summary = %Q{Curate your rich Ruby objects' attributes}
|
13
|
+
s.description = %Q{Define strategies for accessing and displaying a subset of your classes' attributes}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
'LICENSE',
|
16
|
+
'README.rdoc',
|
17
|
+
]
|
18
|
+
|
19
|
+
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.7')
|
20
|
+
s.rubygems_version = '1.3.7'
|
21
|
+
s.specification_version = 3
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ['lib']
|
27
|
+
|
28
|
+
s.add_dependency 'activesupport'
|
29
|
+
s.add_dependency 'actionpack'
|
30
|
+
s.add_dependency 'blockenspiel'
|
31
|
+
s.add_dependency 'conversions'
|
32
|
+
|
33
|
+
s.add_development_dependency 'bundler'
|
34
|
+
s.add_development_dependency 'bueller'
|
35
|
+
s.add_development_dependency 'rake'
|
36
|
+
s.add_development_dependency 'rcov'
|
37
|
+
s.add_development_dependency 'supermodel'
|
38
|
+
end
|
39
|
+
|
data/lib/charisma.rb
CHANGED
@@ -10,7 +10,7 @@ require 'active_support/version'
|
|
10
10
|
end if ActiveSupport::VERSION::MAJOR == 3
|
11
11
|
|
12
12
|
require 'action_pack'
|
13
|
-
require '
|
13
|
+
require 'action_pack/version'
|
14
14
|
%w{
|
15
15
|
action_view/helpers/number_helper
|
16
16
|
}.each do |action_pack_3_requirement|
|
@@ -30,7 +30,6 @@ require 'charisma/curator'
|
|
30
30
|
require 'charisma/curator/curation'
|
31
31
|
require 'charisma/number_helper'
|
32
32
|
|
33
|
-
|
34
33
|
module Charisma
|
35
34
|
def self.included(base)
|
36
35
|
base.send :include, Base
|
@@ -1,19 +1,9 @@
|
|
1
1
|
module Charisma
|
2
|
-
class Characterization
|
3
|
-
attr_reader :characteristics
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
@characteristics = {}
|
7
|
-
end
|
8
|
-
|
9
|
-
def [](name)
|
10
|
-
characteristics[name]
|
11
|
-
end
|
12
|
-
|
2
|
+
class Characterization < Hash
|
13
3
|
include Blockenspiel::DSL
|
14
4
|
def has(name, options = {}, &blk)
|
15
5
|
name = name.to_sym
|
16
|
-
|
6
|
+
self[name] = Characteristic.new(name, options, &blk)
|
17
7
|
end
|
18
8
|
end
|
19
9
|
end
|
data/lib/charisma/curator.rb
CHANGED
@@ -1,11 +1,39 @@
|
|
1
1
|
module Charisma
|
2
2
|
class Curator
|
3
|
-
|
4
|
-
|
3
|
+
attr_reader :subject
|
4
|
+
|
5
|
+
def initialize(subject)
|
6
|
+
@subject = subject
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](key)
|
10
|
+
if value = subject.send(key)
|
11
|
+
Curation.new value, subject.class.characterization[key]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def keys
|
16
|
+
subject.class.characterization.keys.select do |key|
|
17
|
+
!!subject.send(key)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def slice(*only_keys)
|
22
|
+
only_keys.inject({}) do |memo, key|
|
23
|
+
if curation = self[key]
|
24
|
+
memo[key] = curation
|
25
|
+
end
|
26
|
+
memo
|
27
|
+
end
|
5
28
|
end
|
6
29
|
|
7
|
-
def
|
8
|
-
|
30
|
+
def to_hash
|
31
|
+
subject.class.characterization.keys.inject({}) do |memo, key|
|
32
|
+
if curation = self[key]
|
33
|
+
memo[key] = curation
|
34
|
+
end
|
35
|
+
memo
|
36
|
+
end
|
9
37
|
end
|
10
38
|
end
|
11
39
|
end
|
data/lib/charisma/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Charisma
|
2
|
-
VERSION = "0.1.
|
3
|
-
end
|
1
|
+
module Charisma
|
2
|
+
VERSION = "0.1.1"
|
3
|
+
end
|
data/test/helper.rb
CHANGED
@@ -1,63 +1,73 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
$LOAD_PATH.unshift(File.
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
belongs_to :
|
21
|
-
belongs_to :
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
has :
|
27
|
-
has :
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
has :
|
32
|
-
has :
|
33
|
-
has :
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'supermodel'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
require 'charisma'
|
9
|
+
|
10
|
+
class Test::Unit::TestCase
|
11
|
+
end
|
12
|
+
|
13
|
+
class RelativeAstronomicMass < Charisma::Measurement # or don't inherit and provide everything yourself
|
14
|
+
units :hollagrams => 'hg'
|
15
|
+
# before_ and after_ filters or some other reason to use this as opposed to Charisma::Measurements::Mass
|
16
|
+
end
|
17
|
+
|
18
|
+
class Spaceship < SuperModel::Base
|
19
|
+
attributes :window_count, :name, :size, :weight
|
20
|
+
belongs_to :make, :class_name => 'SpaceshipMake', :primary_key => 'name'
|
21
|
+
belongs_to :fuel, :class_name => 'SpaceshipFuel', :primary_key => 'name'
|
22
|
+
belongs_to :destination, :class_name => 'Planet', :primary_key => 'name'
|
23
|
+
|
24
|
+
include Charisma
|
25
|
+
characterize do
|
26
|
+
has :make, :display_with => :name
|
27
|
+
has :fuel
|
28
|
+
has :window_count do |window_count|
|
29
|
+
"#{window_count} windows"
|
30
|
+
end
|
31
|
+
has :size, :measures => :length # uses Charisma::Measurements::Length
|
32
|
+
has :weight, :measures => RelativeAstronomicMass
|
33
|
+
has :name
|
34
|
+
has :destination
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class SpaceshipMake < SuperModel::Base
|
39
|
+
has_many :spaceships
|
40
|
+
attributes :name
|
41
|
+
self.primary_key = :name
|
42
|
+
end
|
43
|
+
|
44
|
+
class SpaceshipFuel < SuperModel::Base
|
45
|
+
has_many :spaceships
|
46
|
+
attributes :name, :emission_factor
|
47
|
+
self.primary_key = :name
|
48
|
+
|
49
|
+
def as_characteristic
|
50
|
+
"#{name} (#{emission_factor} kg CO2/L)"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Planet < SuperModel::Base
|
55
|
+
has_many :spaceships
|
56
|
+
attributes :name
|
57
|
+
self.primary_key = :name
|
58
|
+
|
59
|
+
def to_s
|
60
|
+
name
|
61
|
+
end
|
62
|
+
|
63
|
+
def mass
|
64
|
+
case name
|
65
|
+
when 'Pluto'
|
66
|
+
BigDecimal('1.31e+1_022')
|
67
|
+
else
|
68
|
+
raise "unknown"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
Conversions.register :hollagrams, :supertons, 2
|
data/test/test_charisma.rb
CHANGED
@@ -1,41 +1,70 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestCharisma < Test::Unit::TestCase
|
4
|
-
def
|
5
|
-
spaceship = Spaceship.new :name => 'Amaroq'
|
6
|
-
assert_equal 'Amaroq', spaceship.characteristics[:name].to_s
|
7
|
-
end
|
8
|
-
def
|
9
|
-
spaceship = Spaceship.new :size => 10 # meters
|
10
|
-
assert_equal '10 m', spaceship.characteristics[:size].to_s
|
11
|
-
end
|
12
|
-
def
|
13
|
-
spaceship = Spaceship.new :weight => 1_000_000 # hg
|
14
|
-
assert_equal '1,000,000 hg', spaceship.characteristics[:weight].to_s
|
15
|
-
end
|
16
|
-
def
|
17
|
-
spaceship = Spaceship.new :weight => '1000000.1' # kg
|
18
|
-
assert_equal :hollagrams, spaceship.characteristics[:weight].units
|
19
|
-
assert_equal 'hg', spaceship.characteristics[:weight].u
|
20
|
-
assert_equal 2_000_000.2, spaceship.characteristics[:weight].supertons
|
21
|
-
end
|
22
|
-
def
|
23
|
-
spaceship = Spaceship.new :window_count => 10
|
24
|
-
assert_equal '10 windows', spaceship.characteristics[:window_count].to_s
|
25
|
-
end
|
26
|
-
def
|
27
|
-
planet = Planet.create :name => 'Pluto'
|
28
|
-
spaceship = Spaceship.new :destination => planet
|
29
|
-
assert_equal 'Pluto', spaceship.characteristics[:destination].to_s
|
30
|
-
end
|
31
|
-
def
|
32
|
-
make = SpaceshipMake.create :name => 'Boeing'
|
33
|
-
spaceship = Spaceship.new :make => make
|
34
|
-
assert_equal 'Boeing', spaceship.characteristics[:make].to_s
|
35
|
-
end
|
36
|
-
def
|
37
|
-
fuel = SpaceshipFuel.create :name => 'Kerosene', :emission_factor => 10
|
38
|
-
spaceship = Spaceship.new :fuel => fuel
|
39
|
-
assert_equal 'Kerosene (10 kg CO2/L)', spaceship.characteristics[:fuel].to_s
|
40
|
-
end
|
41
|
-
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestCharisma < Test::Unit::TestCase
|
4
|
+
def test_001_simple_attribute_characteristic
|
5
|
+
spaceship = Spaceship.new :name => 'Amaroq'
|
6
|
+
assert_equal 'Amaroq', spaceship.characteristics[:name].to_s
|
7
|
+
end
|
8
|
+
def test_002_measured_attribute
|
9
|
+
spaceship = Spaceship.new :size => 10 # meters
|
10
|
+
assert_equal '10 m', spaceship.characteristics[:size].to_s
|
11
|
+
end
|
12
|
+
def test_003_classified_attribute
|
13
|
+
spaceship = Spaceship.new :weight => 1_000_000 # hg
|
14
|
+
assert_equal '1,000,000 hg', spaceship.characteristics[:weight].to_s
|
15
|
+
end
|
16
|
+
def test_004_units
|
17
|
+
spaceship = Spaceship.new :weight => '1000000.1' # kg
|
18
|
+
assert_equal :hollagrams, spaceship.characteristics[:weight].units
|
19
|
+
assert_equal 'hg', spaceship.characteristics[:weight].u
|
20
|
+
assert_equal 2_000_000.2, spaceship.characteristics[:weight].supertons
|
21
|
+
end
|
22
|
+
def test_005_attribute_with_custom_display
|
23
|
+
spaceship = Spaceship.new :window_count => 10
|
24
|
+
assert_equal '10 windows', spaceship.characteristics[:window_count].to_s
|
25
|
+
end
|
26
|
+
def test_006_simple_association
|
27
|
+
planet = Planet.create :name => 'Pluto'
|
28
|
+
spaceship = Spaceship.new :destination => planet
|
29
|
+
assert_equal 'Pluto', spaceship.characteristics[:destination].to_s
|
30
|
+
end
|
31
|
+
def test_007_association_with_display_hint
|
32
|
+
make = SpaceshipMake.create :name => 'Boeing'
|
33
|
+
spaceship = Spaceship.new :make => make
|
34
|
+
assert_equal 'Boeing', spaceship.characteristics[:make].to_s
|
35
|
+
end
|
36
|
+
def test_008_association_with_prepared_display
|
37
|
+
fuel = SpaceshipFuel.create :name => 'Kerosene', :emission_factor => 10
|
38
|
+
spaceship = Spaceship.new :fuel => fuel
|
39
|
+
assert_equal 'Kerosene (10 kg CO2/L)', spaceship.characteristics[:fuel].to_s
|
40
|
+
end
|
41
|
+
def test_009_number_helper
|
42
|
+
assert_equal '1,000', Charisma::NumberHelper.delimit(1_000)
|
43
|
+
end
|
44
|
+
def test_010_characteristics_slice
|
45
|
+
spaceship = Spaceship.new :name => 'Amaroq'
|
46
|
+
assert_equal 'Amaroq', spaceship.characteristics.slice(:name)[:name].to_s
|
47
|
+
end
|
48
|
+
def test_011_associated_object_methods
|
49
|
+
planet = Planet.create :name => 'Pluto'
|
50
|
+
spaceship = Spaceship.new :destination => planet
|
51
|
+
assert_equal BigDecimal('1.31e+1_022'), spaceship.characteristics[:destination].mass
|
52
|
+
end
|
53
|
+
def test_012_missing_characteristics_come_back_as_nil
|
54
|
+
spaceship = Spaceship.new :name => 'Amaroq'
|
55
|
+
assert_equal nil, spaceship.characteristics[:size]
|
56
|
+
end
|
57
|
+
def test_013_characteristics_keys
|
58
|
+
spaceship = Spaceship.new :name => 'Amaroq'
|
59
|
+
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
|
+
end
|
63
|
+
def test_014_characterization_keys
|
64
|
+
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
|
+
end
|
66
|
+
def test_015_to_hash_preserving_curation
|
67
|
+
spaceship = Spaceship.new :name => 'Amaroq'
|
68
|
+
assert_equal 'Amaroq', spaceship.characteristics.to_hash[:name].to_s
|
69
|
+
end
|
70
|
+
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: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
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-04-29 00:00:00 -04:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|