representable 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ h2. 1.4.1
2
+
3
+ * Added `:representer_exec` to have lambdas be executed in decorator instance context.
4
+
1
5
  h2. 1.4.0
2
6
 
3
7
  * We now have two strategies for representing: the old extend approach and the brand-new decorator which leaves represented objects untouched. See "README":https://github.com/apotonick/representable#decorator-vs-extend for details.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 - 2013 Nick Sutterer and the roar contributors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -558,10 +558,17 @@ Use the `:type` option to specify the conversion target. Note that `:default` st
558
558
 
559
559
  (Please don't read this section!)
560
560
 
561
- If you need a special binding for a property you're free to create it using the `:binding` option.
561
+ * If you need a special binding for a property you're free to create it using the `:binding` option.
562
562
 
563
563
  property :title, :binding => lambda { |*args| JSON::TitleBinding.new(*args) }
564
564
 
565
+ * Lambdas are usually executed in the represented object's context. If your writing a `Decorator` representer and you need to execute lambdas in its context use the `:representer_exec` option.
566
+
567
+ class SongRepresenter < Representable::Decorator
568
+ property :title, :representer_exec => true, :getter => lambda {..}
569
+ end
570
+
571
+ You can still access the represented object in the lambda using `represented`. In a module representer this option is ignored.
565
572
 
566
573
  ## Copyright
567
574
 
@@ -569,3 +576,5 @@ Representable started as a heavily simplified fork of the ROXML gem. Big thanks
569
576
 
570
577
  * Copyright (c) 2011-2013 Nick Sutterer <apotonick@gmail.com>
571
578
  * ROXML is Copyright (c) 2004-2009 Ben Woosley, Zak Mandhro and Anders Engstrom.
579
+
580
+ Representable is released under the [MIT License](http://www.opensource.org/licenses/MIT).
@@ -16,13 +16,14 @@ module Representable
16
16
  raise "Binding#definition is no longer supported as all Definition methods are now delegated automatically."
17
17
  end
18
18
 
19
- def initialize(definition, represented, user_options={}) # TODO: remove default arg.
19
+ def initialize(definition, represented, user_options={}, lambda_context=represented) # TODO: remove default arg for user options. # DISCUSS: make lambda_context an options hash?
20
20
  super(definition)
21
- @represented = represented
22
- @user_options = user_options
21
+ @represented = represented
22
+ @user_options = user_options
23
+ @lambda_context = lambda_context
23
24
  end
24
25
 
25
- attr_reader :user_options, :represented # TODO: make private/remove.
26
+ attr_reader :user_options, :represented, :lambda_context # TODO: make private/remove.
26
27
 
27
28
  # Main entry point for rendering/parsing a property object.
28
29
  def serialize(value)
@@ -89,7 +90,7 @@ module Representable
89
90
  # Execute the block for +option_name+ on the represented object.
90
91
  def represented_exec_for(option_name, *args)
91
92
  return unless options[option_name]
92
- represented.instance_exec(*args+[user_options], &options[option_name])
93
+ lambda_context.instance_exec(*args+[user_options], &options[option_name])
93
94
  end
94
95
 
95
96
 
@@ -13,7 +13,9 @@ module Representable
13
13
  end
14
14
 
15
15
  def representable_binding_for(attr, format, options)
16
- format.build(attr, represented, options)
16
+ context = attr.options[:representer_exec] ? self : represented # DISCUSS: should Decorator know this kinda stuff?
17
+
18
+ format.build(attr, represented, options, context)
17
19
  end
18
20
  end
19
21
  end
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "1.4.0"
2
+ VERSION = "1.4.1"
3
3
  end
@@ -674,6 +674,45 @@ class RepresentableTest < MiniTest::Spec
674
674
  end
675
675
  end
676
676
 
677
+ describe ":representer_exec" do
678
+ representer! do
679
+ property :title, :getter => lambda { |*| title_from_representer }, :representer_exec => true
680
+ end
681
+
682
+ it "executes lambdas in represented context" do
683
+ Class.new do
684
+ def title_from_representer
685
+ "Sounds Of Silence"
686
+ end
687
+ end.new.extend(representer).to_hash.must_equal({"title"=>"Sounds Of Silence"})
688
+ end
689
+
690
+ describe "with decorator" do
691
+
692
+ it "executes lambdas in representer context" do
693
+ rpr = representer
694
+ Class.new(Representable::Decorator) do
695
+ include rpr
696
+
697
+ def title_from_representer
698
+ "Sounds Of Silence"
699
+ end
700
+ end.new(Object.new).to_hash().must_equal({"title"=>"Sounds Of Silence"})
701
+ end
702
+
703
+ it "still allows accessing the represented object" do
704
+ Class.new(Representable::Decorator) do
705
+ include Representable::Hash
706
+ property :title, :getter => lambda { |*| represented.title }, :representer_exec => true
707
+
708
+ def title
709
+ "Sounds Of Silence"
710
+ end
711
+ end.new(OpenStruct.new(:title => "Secret")).to_hash.must_equal({"title"=>"Secret"})
712
+ end
713
+ end
714
+ end
715
+
677
716
  describe "decorator" do
678
717
  # TODO: Move to global place since it's used twice.
679
718
  class SongRepresentation < Representable::Decorator
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: 1.4.0
4
+ version: 1.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-10 00:00:00.000000000 Z
12
+ date: 2013-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -167,6 +167,7 @@ files:
167
167
  - .travis.yml
168
168
  - CHANGES.textile
169
169
  - Gemfile
170
+ - LICENSE
170
171
  - README.md
171
172
  - Rakefile
172
173
  - TODO
@@ -225,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
226
  version: '0'
226
227
  requirements: []
227
228
  rubyforge_project:
228
- rubygems_version: 1.8.24
229
+ rubygems_version: 1.8.25
229
230
  signing_key:
230
231
  specification_version: 3
231
232
  summary: Maps representation documents from and to Ruby objects. Includes XML and