serializable_decorator 0.0.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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format progress
3
+ --drb
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in serializable-decorator.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'spork'
9
+ gem 'autotest'
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andy Caldwell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # SerializableDecorator
2
+
3
+ An implementation of Delegate that groups the member variables of the delegator (a subclass of SerializableDecorator) together with those of the delegated object. For example:
4
+
5
+ class MyDecorator < SerializableDecorator
6
+ :attr_accessor :foo
7
+ end
8
+
9
+ class SomeClass
10
+ :attr_accessor :bar
11
+ end
12
+
13
+ decorated_class = MyDecorator.new(SomeClass.new)
14
+ decorated_class.foo = "Hello"
15
+ decorated_class.bar = "World"
16
+ puts decorated_class.foo # => "Hello"
17
+ puts decorated_class.bar # => "World"
18
+ puts decorated_class.instance_variables # => @foo and @bar
19
+ puts decorated_class.instance_values # => { "foo" => "Hello", "bar" => "World" }
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ gem 'serializable_decorator'
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install serializable_decorator
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module Delegators
2
+ module SerializableDecorator
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,45 @@
1
+ require 'serializable_decorator/version'
2
+ require 'delegate'
3
+ require 'json' unless Object.method_defined?(:to_json)
4
+
5
+ class SerializableDecorator < Delegator
6
+
7
+ @@decorator_objects = {}
8
+
9
+ def self.change_obj(decorator, obj)
10
+ @@decorator_objects[decorator.object_id] = obj
11
+ end
12
+
13
+ def self.get_obj(decorator)
14
+ @@decorator_objects[decorator.object_id]
15
+ end
16
+
17
+ def initialize(obj)
18
+ super
19
+ SerializableDecorator.change_obj(self, obj)
20
+ end
21
+
22
+ def __getobj__
23
+ SerializableDecorator.get_obj(self)
24
+ end
25
+
26
+ def __setobj__(obj)
27
+ SerializableDecorator.change_obj(self, obj)
28
+ end
29
+
30
+ def instance_variables
31
+ super + __getobj__.instance_variables
32
+ end
33
+
34
+ def instance_values
35
+ Hash[instance_variables.map { | name | [ name.to_s[1..-1], instance_variable_get(name) ] } ]
36
+ end
37
+
38
+ def instance_variable_get(name)
39
+ __getobj__.instance_variable_get(name) || super
40
+ end
41
+
42
+ def to_json
43
+ instance_values.to_json
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/serializable_decorator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Andy Caldwell"]
6
+ gem.email = ["andy.m.caldwell@googlemail.com"]
7
+ gem.description = %q{A serializable decorator}
8
+ gem.summary = %q{A concrete decorator implementation that can be serialized to JSON with the decoration included alongside the original attributes}
9
+ gem.homepage = "https://github.com/bossmc/serializable-decorator"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "serializable_decorator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Delegators::SerializableDecorator::VERSION
17
+
18
+ gem.add_runtime_dependency "json"
19
+
20
+ gem.add_development_dependency "rspec"
21
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ class Test
4
+ attr_accessor :foo
5
+ end
6
+
7
+ class TestDecorator < SerializableDecorator
8
+ attr_accessor :bar
9
+ end
10
+
11
+ describe SerializableDecorator do
12
+ before(:each) do
13
+ @td = TestDecorator.new(Test.new)
14
+ end
15
+
16
+ it "Original attributes are still available" do
17
+ @td.foo.should be_nil
18
+ @td.foo = 12
19
+ @td.foo.should == 12
20
+ end
21
+
22
+ it "Decoration attributes should be available" do
23
+ @td.bar.should be_nil
24
+ @td.bar = 13
25
+ @td.bar.should == 13
26
+ end
27
+
28
+ it "Supports changing the decorated object" do
29
+ @td.foo = 12
30
+ @td.bar = 13
31
+ @td.__setobj__(Test.new)
32
+ @td.foo.should be_nil
33
+ @td.bar.should == 13
34
+ end
35
+
36
+ it "Can get list of attributes" do
37
+ @td.foo = 12
38
+ @td.bar = 13
39
+ @td.instance_variables.should include(:@foo)
40
+ @td.instance_variables.should include(:@bar)
41
+ end
42
+
43
+ it "Can retrieve original attributes by name" do
44
+ @td.foo = 12
45
+ @td.instance_variable_get("@foo").should == 12
46
+ end
47
+
48
+ it "Can retrieve decoration attributes by name" do
49
+ @td.bar = 13
50
+ @td.instance_variable_get("@bar").should == 13
51
+ end
52
+
53
+ it "Can retrieve all local variables with values" do
54
+ @td.foo = 12
55
+ @td.bar = 13
56
+ @td.instance_values.should include("foo" => 12)
57
+ @td.instance_values.should include("bar" => 13)
58
+ end
59
+
60
+ it "Can serialize original attributes to JSON" do
61
+ @td.foo = 12
62
+ @td.to_json.should include('"foo":12')
63
+ end
64
+
65
+ it "Can serialize decorated attributes to JSON" do
66
+ @td.bar = 13
67
+ @td.to_json.should include('"bar":13')
68
+ end
69
+
70
+ it "Should not report any extraneous variables" do
71
+ @td.instance_variables.should be_empty
72
+ @td.instance_values.should be_empty
73
+ end
74
+
75
+ it "Should not include any extraneous entries in the JSON output" do
76
+ @td.to_json.should == "{}"
77
+ end
78
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+ require 'rspec'
4
+ #uncomment the following line to use spork with the debugger
5
+ #require 'spork/ext/ruby-debug'
6
+
7
+ Spork.prefork do
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+ end
13
+ end
14
+
15
+ Spork.each_run do
16
+ # This code will be run each time you run your specs.
17
+ require 'serializable_decorator'
18
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serializable_decorator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andy Caldwell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A serializable decorator
47
+ email:
48
+ - andy.m.caldwell@googlemail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - lib/serializable_decorator.rb
60
+ - lib/serializable_decorator/version.rb
61
+ - serializable-decorator.gemspec
62
+ - spec/serializable_decorator_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: https://github.com/bossmc/serializable-decorator
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A concrete decorator implementation that can be serialized to JSON with the
88
+ decoration included alongside the original attributes
89
+ test_files:
90
+ - spec/serializable_decorator_spec.rb
91
+ - spec/spec_helper.rb