representable 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ h2. 0.10.2
2
+
3
+ * Added `representable_property :accessors => false` option to suppress adding accessors.
4
+ * @Representable.representation_wrap@ is no longer inherited.
5
+ * Representers can now be defined in modules. They inherit to including modules.
6
+
1
7
  h2. 0.10.1
2
8
 
3
9
  * The block in @to_*@ and @from_*@ now yields the symbolized property name. If you need the binding/definition you gotta get it yourself.
@@ -37,6 +37,22 @@ Since you keep forgetting the heroes of your childhood you decide to implement a
37
37
 
38
38
  This declares two simple properties. Representable will automatically add accessors to the class.
39
39
 
40
+ If you don't want declarations in your models, use a module.
41
+
42
+ module HeroRepresentation
43
+ include Representable
44
+
45
+ representable_property :forename
46
+ representable_property :surename
47
+ end
48
+
49
+ This module needs a host class to be used with.
50
+
51
+ class Hero
52
+ include Representable::JSON
53
+ include HeroRepresentation
54
+ end
55
+
40
56
  == Rendering
41
57
 
42
58
  Now let's create and render our first hero.
@@ -1,4 +1,3 @@
1
- require 'hooks/inheritable_attribute'
2
1
  require 'representable/definition'
3
2
 
4
3
  module Representable
@@ -7,11 +6,9 @@ module Representable
7
6
  extend ClassMethods::Declarations
8
7
  extend ClassMethods::Accessors
9
8
 
10
- extend Hooks::InheritableAttribute
11
- inheritable_attr :representable_attrs
12
- self.representable_attrs = []
13
-
14
- inheritable_attr :representable_wrap
9
+ def self.included(base)
10
+ base.representable_attrs.push(*representable_attrs) # "inherit".
11
+ end
15
12
  end
16
13
  end
17
14
 
@@ -64,10 +61,12 @@ private
64
61
  # representable_property :name
65
62
  # representable_property :name, :from => :title
66
63
  # representable_property :name, :as => Name
67
- def representable_property(*args)
68
- attr = add_representable_property(*args)
69
- attr_reader(attr.getter)
70
- attr_writer(attr.getter)
64
+ # representable_property :name, :accessors => false
65
+ def representable_property(name, options={})
66
+ attr = add_representable_property(name, options)
67
+
68
+ attr_reader(attr.getter) unless options[:accessors] == false
69
+ attr_writer(attr.getter) unless options[:accessors] == false
71
70
  end
72
71
 
73
72
  # Declares a represented document node collection.
@@ -91,6 +90,18 @@ private
91
90
  end
92
91
 
93
92
  module Accessors
93
+ def representable_attrs
94
+ @representable_attrs ||= []
95
+ end
96
+
97
+ def representable_wrap
98
+ @representable_wrap ||= false
99
+ end
100
+
101
+ def representable_wrap=(name)
102
+ @representable_wrap = name
103
+ end
104
+
94
105
  def representation_wrap=(name)
95
106
  self.representable_wrap = name
96
107
  end
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "0.10.1"
2
+ VERSION = "0.10.2"
3
3
  end
@@ -11,15 +11,14 @@ Gem::Specification.new do |s|
11
11
  s.authors = ["Nick Sutterer"]
12
12
  s.email = ["apotonick@gmail.com"]
13
13
  s.homepage = "http://representable.apotomo.de"
14
- s.summary = %q{Maps representation documents from and to Ruby objects. Includes XML and JSON support, plain properties and compositions.}
15
- s.description = %q{Maps representation documents from and to Ruby objects. Includes XML and JSON support, plain properties and compositions.}
14
+ s.summary = %q{Maps representation documents from and to Ruby objects. Includes XML and JSON support, plain properties, collections and compositions.}
15
+ s.description = %q{Maps representation documents from and to Ruby objects. Includes XML and JSON support, plain properties, collections and compositions.}
16
16
 
17
17
  s.files = `git ls-files`.split("\n")
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency "hooks"
23
22
  s.add_dependency "nokogiri"
24
23
  s.add_dependency "json"
25
24
 
@@ -10,6 +10,19 @@ class RepresentableTest < MiniTest::Spec
10
10
  representable_property :street_cred
11
11
  end
12
12
 
13
+ module BandRepresentation
14
+ include Representable
15
+
16
+ representable_property :name
17
+ end
18
+
19
+ module PunkBandRepresentation
20
+ include Representable
21
+ include BandRepresentation
22
+
23
+ representable_property :street_cred
24
+ end
25
+
13
26
 
14
27
  describe "#representable_attrs" do
15
28
  it "responds to #representable_attrs" do
@@ -17,10 +30,40 @@ class RepresentableTest < MiniTest::Spec
17
30
  assert_equal "name", Band.representable_attrs.first.name
18
31
  end
19
32
 
20
- it "inherits correctly" do
21
- assert_equal 2, PunkBand.representable_attrs.size
22
- assert_equal "name", PunkBand.representable_attrs.first.name
23
- assert_equal "street_cred", PunkBand.representable_attrs.last.name
33
+ describe "in module" do
34
+ it "returns definitions" do
35
+ assert_equal 1, BandRepresentation.representable_attrs.size
36
+ assert_equal "name", BandRepresentation.representable_attrs.first.name
37
+ end
38
+
39
+ it "inherits to including modules" do
40
+ assert_equal 2, PunkBandRepresentation.representable_attrs.size
41
+ assert_equal "name", PunkBandRepresentation.representable_attrs.first.name
42
+ assert_equal "street_cred", PunkBandRepresentation.representable_attrs.last.name
43
+ end
44
+
45
+ it "inherits to including class" do
46
+ band = Class.new do
47
+ include Representable
48
+ include PunkBandRepresentation
49
+ end
50
+
51
+ assert_equal 2, band.representable_attrs.size
52
+ assert_equal "name", band.representable_attrs.first.name
53
+ assert_equal "street_cred", band.representable_attrs.last.name
54
+ end
55
+
56
+ it "allows including the concrete representer module later" do
57
+ require 'representable/json'
58
+ vd = class VD
59
+ include Representable::JSON
60
+ include PunkBandRepresentation
61
+ end.new
62
+ vd.name = "Vention Dention"
63
+ vd.street_cred = 1
64
+ assert_equal "{\"name\":\"Vention Dention\",\"street_cred\":1}", vd.to_json
65
+ end
66
+
24
67
  end
25
68
  end
26
69
 
@@ -44,6 +87,15 @@ class RepresentableTest < MiniTest::Spec
44
87
  assert_equal "friends", band.representable_attrs.last.from
45
88
  end
46
89
  end
90
+
91
+ describe ":accessor" do
92
+ it "doesn't add methods when false" do
93
+ klass = Class.new(Band) { representable_property :friends, :accessors => false }
94
+ band = klass.new
95
+ assert ! band.respond_to?(:friends)
96
+ assert ! band.respond_to?("friends=")
97
+ end
98
+ end
47
99
  end
48
100
 
49
101
  describe "#representable_collection" do
@@ -74,7 +126,6 @@ class RepresentableTest < MiniTest::Spec
74
126
  assert_equal nil, SoundSystem.representation_wrap
75
127
  end
76
128
 
77
-
78
129
  it "infers a printable class name if set to true" do
79
130
  HardcoreBand.representation_wrap = true
80
131
  assert_equal "hardcore_band", HardcoreBand.send(:representation_wrap)
@@ -85,9 +136,9 @@ class RepresentableTest < MiniTest::Spec
85
136
  assert_equal "breach", HardcoreBand.representation_wrap
86
137
  end
87
138
 
88
- it "inherits correctly" do
139
+ it "doesn't inherit correctly" do
89
140
  HardcoreBand.representation_wrap = "breach"
90
- assert_equal "breach", SoftcoreBand.representation_wrap
141
+ assert_equal nil, SoftcoreBand.representation_wrap
91
142
  end
92
143
  end
93
144
 
@@ -125,7 +125,11 @@ class XmlTest < MiniTest::Spec
125
125
  end
126
126
 
127
127
  it "respects #representation_wrap=" do
128
- klass = Class.new(Band)
128
+ klass = Class.new(Band) do
129
+ include Representable
130
+ representable_property :name
131
+ end
132
+
129
133
  klass.representation_wrap = :group
130
134
  assert_xml_equal "<group><name>Rise Against</name></group>", klass.new("Rise Against").to_node.to_s
131
135
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.10.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-01 00:00:00.000000000Z
12
+ date: 2011-12-03 00:00:00.000000000Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: hooks
16
- requirement: &72137670 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *72137670
25
14
  - !ruby/object:Gem::Dependency
26
15
  name: nokogiri
27
- requirement: &72137450 !ruby/object:Gem::Requirement
16
+ requirement: &75503660 !ruby/object:Gem::Requirement
28
17
  none: false
29
18
  requirements:
30
19
  - - ! '>='
@@ -32,10 +21,10 @@ dependencies:
32
21
  version: '0'
33
22
  type: :runtime
34
23
  prerelease: false
35
- version_requirements: *72137450
24
+ version_requirements: *75503660
36
25
  - !ruby/object:Gem::Dependency
37
26
  name: json
38
- requirement: &72137230 !ruby/object:Gem::Requirement
27
+ requirement: &75503430 !ruby/object:Gem::Requirement
39
28
  none: false
40
29
  requirements:
41
30
  - - ! '>='
@@ -43,10 +32,10 @@ dependencies:
43
32
  version: '0'
44
33
  type: :runtime
45
34
  prerelease: false
46
- version_requirements: *72137230
35
+ version_requirements: *75503430
47
36
  - !ruby/object:Gem::Dependency
48
37
  name: rake
49
- requirement: &72137010 !ruby/object:Gem::Requirement
38
+ requirement: &75503180 !ruby/object:Gem::Requirement
50
39
  none: false
51
40
  requirements:
52
41
  - - ! '>='
@@ -54,10 +43,10 @@ dependencies:
54
43
  version: '0'
55
44
  type: :development
56
45
  prerelease: false
57
- version_requirements: *72137010
46
+ version_requirements: *75503180
58
47
  - !ruby/object:Gem::Dependency
59
48
  name: test_xml
60
- requirement: &72136800 !ruby/object:Gem::Requirement
49
+ requirement: &75502870 !ruby/object:Gem::Requirement
61
50
  none: false
62
51
  requirements:
63
52
  - - ! '>='
@@ -65,10 +54,10 @@ dependencies:
65
54
  version: '0'
66
55
  type: :development
67
56
  prerelease: false
68
- version_requirements: *72136800
57
+ version_requirements: *75502870
69
58
  - !ruby/object:Gem::Dependency
70
59
  name: minitest
71
- requirement: &72136530 !ruby/object:Gem::Requirement
60
+ requirement: &75502410 !ruby/object:Gem::Requirement
72
61
  none: false
73
62
  requirements:
74
63
  - - ~>
@@ -76,10 +65,10 @@ dependencies:
76
65
  version: '2.8'
77
66
  type: :development
78
67
  prerelease: false
79
- version_requirements: *72136530
68
+ version_requirements: *75502410
80
69
  - !ruby/object:Gem::Dependency
81
70
  name: mocha
82
- requirement: &72136320 !ruby/object:Gem::Requirement
71
+ requirement: &75501950 !ruby/object:Gem::Requirement
83
72
  none: false
84
73
  requirements:
85
74
  - - ! '>='
@@ -87,9 +76,9 @@ dependencies:
87
76
  version: '0'
88
77
  type: :development
89
78
  prerelease: false
90
- version_requirements: *72136320
79
+ version_requirements: *75501950
91
80
  description: Maps representation documents from and to Ruby objects. Includes XML
92
- and JSON support, plain properties and compositions.
81
+ and JSON support, plain properties, collections and compositions.
93
82
  email:
94
83
  - apotonick@gmail.com
95
84
  executables: []
@@ -143,5 +132,5 @@ rubygems_version: 1.8.10
143
132
  signing_key:
144
133
  specification_version: 3
145
134
  summary: Maps representation documents from and to Ruby objects. Includes XML and
146
- JSON support, plain properties and compositions.
135
+ JSON support, plain properties, collections and compositions.
147
136
  test_files: []