representation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'activerecord', '~> 3.0'
4
+ gem 'activesupport', '~> 3.0'
5
+
6
+ group :development do
7
+ gem 'rspec'
8
+ gem 'bundler', '~> 1.0.0'
9
+ gem 'jeweler', '~> 1.6.4'
10
+ gem 'sqlite3-ruby'
11
+ gem 'ruby-debug19'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.0)
5
+ activesupport (= 3.0.0)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4.1)
8
+ activerecord (3.0.0)
9
+ activemodel (= 3.0.0)
10
+ activesupport (= 3.0.0)
11
+ arel (~> 1.0.0)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.0)
14
+ archive-tar-minitar (0.5.2)
15
+ arel (1.0.1)
16
+ activesupport (~> 3.0.0)
17
+ builder (2.1.2)
18
+ columnize (0.3.4)
19
+ diff-lcs (1.1.2)
20
+ git (1.2.5)
21
+ i18n (0.4.2)
22
+ jeweler (1.6.4)
23
+ bundler (~> 1.0)
24
+ git (>= 1.2.5)
25
+ rake
26
+ linecache19 (0.5.12)
27
+ ruby_core_source (>= 0.1.4)
28
+ rake (0.9.2)
29
+ rspec (2.5.0)
30
+ rspec-core (~> 2.5.0)
31
+ rspec-expectations (~> 2.5.0)
32
+ rspec-mocks (~> 2.5.0)
33
+ rspec-core (2.5.1)
34
+ rspec-expectations (2.5.0)
35
+ diff-lcs (~> 1.1.2)
36
+ rspec-mocks (2.5.0)
37
+ ruby-debug-base19 (0.11.25)
38
+ columnize (>= 0.3.1)
39
+ linecache19 (>= 0.5.11)
40
+ ruby_core_source (>= 0.1.4)
41
+ ruby-debug19 (0.11.6)
42
+ columnize (>= 0.3.1)
43
+ linecache19 (>= 0.5.11)
44
+ ruby-debug-base19 (>= 0.11.19)
45
+ ruby_core_source (0.1.5)
46
+ archive-tar-minitar (>= 0.5.2)
47
+ sqlite3-ruby (1.3.2)
48
+ tzinfo (0.3.29)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ activerecord (~> 3.0)
55
+ activesupport (~> 3.0)
56
+ bundler (~> 1.0.0)
57
+ jeweler (~> 1.6.4)
58
+ rspec
59
+ ruby-debug19
60
+ sqlite3-ruby
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Chris Johnson, SocialVibe
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.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ = representation
2
+
3
+ Create different, named representations of a resource for cleaner state representation.
4
+
5
+ = Install
6
+
7
+ gem 'representation'
8
+ bundle
9
+
10
+ == Contributing to representation
11
+
12
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
13
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
14
+ * Fork the project
15
+ * Start a feature/bugfix branch
16
+ * Commit and push until you are happy with your contribution
17
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
18
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
19
+
20
+ == Copyright
21
+
22
+ Copyright (c) 2011 Chris Johnson, SocialVibe. See LICENSE.txt for further details.
23
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "representation"
18
+ gem.homepage = "http://github.com/findchris/representation"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Model logical groups of attributes as different object representations.}
21
+ gem.description = %Q{Create different, named representations of a resource for cleaner state representation.}
22
+ gem.email = "findchris@gmail.com"
23
+ gem.authors = ["Chris Johnson"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "representation #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,52 @@
1
+ require 'active_record'
2
+
3
+ module Representation
4
+ module ActiveRecord
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :representations
9
+ self.representations = {}
10
+ end
11
+
12
+ module ClassMethods
13
+ def representation(name, *attributes_and_method_names)
14
+ representations[name] = attributes_and_method_names
15
+ end
16
+ def representation_names
17
+ representations.keys
18
+ end
19
+ def values_for_representation(name)
20
+ representations[name]
21
+ end
22
+ end
23
+
24
+ module InstanceMethods
25
+ def representation(name)
26
+ attributes_and_method_names = self.class.values_for_representation(name)
27
+ represented_attributes = attributes_and_method_names.inject({}) do |attributes, val|
28
+ attributes.merge({val.to_s => send(val)})
29
+ end
30
+
31
+ clone.tap do |represented_object|
32
+ represented_object.instance_variable_set('@attributes', represented_attributes)
33
+
34
+ represented_attributes.each do |key, value|
35
+ represented_object.singleton_class.send :attr_accessor, key.to_sym
36
+ represented_object.send "#{key}=".to_sym, value
37
+ end
38
+
39
+ def represented_object.attributes
40
+ @attributes
41
+ end
42
+
43
+ def represented_object.inspect
44
+ attributes_as_nice_string = @attributes.keys.map {|name| "#{name}: #{attribute_for_inspect(name)}"}
45
+ "#<#{self.class} #{attributes_as_nice_string.compact.join(", ")}>"
46
+ end
47
+ end
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,14 @@
1
+ require 'representation/active_record'
2
+ ActiveRecord::Base.send :include, Representation::ActiveRecord
3
+
4
+ # TODO: Maybe use a Railtie, e.g.:
5
+ # module Representation
6
+ # class Railtie < Rails::Railtie
7
+ # initializer "representation.active_record" do
8
+ # ActiveSupport.on_load :active_record do
9
+ # require 'representation/active_record'
10
+ # include Representation::ActiveRecord
11
+ # end
12
+ # end
13
+ # end
14
+ # end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'representation'
@@ -0,0 +1,127 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'ruby-debug'
3
+
4
+ describe "Representation" do
5
+ context "the ActiveRecord module" do
6
+ it "should be includable" do
7
+ class Dummy
8
+ include Representation::ActiveRecord
9
+ end
10
+ end
11
+ it "should make available the representation class method" do
12
+ class Dummy
13
+ include Representation::ActiveRecord
14
+ representation :ignored, :ignored
15
+ attr_accessor :ignored
16
+ end
17
+ end
18
+ it "should make available the representation instance method" do
19
+ class Dummy
20
+ include Representation::ActiveRecord
21
+ representation :ignored, :ignored
22
+ attr_accessor :ignored
23
+ end
24
+ Dummy.new.representation(:ignored)
25
+ end
26
+ end
27
+ context ".representation" do
28
+ it "should accept the name of the representation as the first argument" do
29
+ class Dummy
30
+ include Representation::ActiveRecord
31
+ representation :public, :ignored
32
+ end
33
+ Dummy.representation_names.should include(:public)
34
+ end
35
+ it "should accept a list of attributes and method names that make up the named representation" do
36
+ class Dummy
37
+ include Representation::ActiveRecord
38
+ representation :public, :name, :calculated_age
39
+
40
+ attr_accessor :name, :age
41
+
42
+ def calculated_age
43
+ age * 2
44
+ end
45
+ end
46
+ Dummy.values_for_representation(:public).should == [:name, :calculated_age]
47
+ end
48
+ end
49
+ context "#representation" do
50
+ it "should return the same type of object as the receiver" do
51
+ user = User.new(:age => 42)
52
+ user.representation(:public).should be_an_instance_of(User)
53
+ end
54
+ it "should return an object with only the attributes identified by the representation definition" do
55
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
56
+ public_user = user.representation(:public)
57
+
58
+ public_user.name.should == 'Tweedle Dum'
59
+ public_user.calculated_age.should == 84
60
+ lambda { public_user.ssn }.should raise_error(NoMethodError)
61
+ end
62
+ end
63
+ context "when working with the representation object" do
64
+ it "should print an accurate #inspect string" do
65
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
66
+ public_user = user.representation(:public)
67
+ public_user.inspect.should == '#<User name: "Tweedle Dum", calculated_age: 84>'
68
+ end
69
+ it "should have the @attributes ivar set correctly" do
70
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
71
+ public_user = user.representation(:public)
72
+ public_user.instance_variable_get('@attributes').should == {'name' => 'Tweedle Dum', 'calculated_age' => 84}
73
+ end
74
+ it "should have have the #attributes method defined correctly" do
75
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
76
+ public_user = user.representation(:public)
77
+ public_user.attributes.should == {'name' => 'Tweedle Dum', 'calculated_age' => 84}
78
+ end
79
+ it "should have attr_accessors set for each attribute of the representation" do
80
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
81
+ public_user = user.representation(:public)
82
+ public_user.should respond_to(:name=)
83
+ public_user.name.should == 'Tweedle Dum'
84
+ public_user.should respond_to(:calculated_age=)
85
+ public_user.calculated_age.should == 84
86
+ end
87
+ it "should allow multiple representations" do
88
+ user = User.create!(:name => 'Tweedle Dum', :age => 42, :ssn => '555-55-5555')
89
+ public_user = user.representation(:public)
90
+ internal_user = user.representation(:internal)
91
+ public_user.attributes.should == {'name' => 'Tweedle Dum', 'calculated_age' => 84}
92
+ internal_user.attributes.should == {'id' => 1, 'name' => 'Tweedle Dum', 'age' => 42, 'ssn' => '555-55-5555'}
93
+ end
94
+ it "should respond to to_json" do
95
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
96
+ public_user = user.representation(:public)
97
+ public_user.should respond_to :to_json
98
+ end
99
+ it "should respond to to_xml" do
100
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
101
+ public_user = user.representation(:public)
102
+ public_user.should respond_to :to_xml
103
+ end
104
+ it "should respond to as_json" do
105
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
106
+ public_user = user.representation(:public)
107
+ public_user.should respond_to :as_json
108
+ end
109
+ it "should respond to serializable_hash" do
110
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
111
+ public_user = user.representation(:public)
112
+ public_user.should respond_to :serializable_hash
113
+ end
114
+ it "should not modify the resource off of which the representation is based" do
115
+ user = User.new(:name => 'Tweedle Dum', :age => 42)
116
+ public_user = user.representation(:public)
117
+ public_user.name = 'Tweedle Dee'
118
+ user.name.should == 'Tweedle Dum'
119
+ end
120
+ end
121
+ context "ActiveRecord" do
122
+ it "should include the Representation module into ActiveRecord" do
123
+ class SomeModel < ActiveRecord::Base; end
124
+ SomeModel.should respond_to :representation
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'representation'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ # Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+ load File.dirname(__FILE__) + '/support/connection.rb'
10
+ load File.dirname(__FILE__) + '/support/schema.rb'
11
+ load File.dirname(__FILE__) + '/support/models.rb'
12
+
13
+ RSpec.configure do |config|
14
+
15
+ end
16
+
@@ -0,0 +1,4 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => "sqlite3",
3
+ :database => File.dirname(__FILE__) + "/representation.sqlite3"
4
+ )
@@ -0,0 +1,9 @@
1
+ class User < ActiveRecord::Base
2
+ include Representation
3
+ representation :public, :name, :calculated_age
4
+ representation :internal, :id, :name, :ssn, :age
5
+
6
+ def calculated_age
7
+ age * 2
8
+ end
9
+ end
Binary file
@@ -0,0 +1,10 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :users, :force => true do |t|
5
+ t.string :name
6
+ t.integer :age
7
+ t.string :ssn
8
+ t.timestamps
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: representation
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Chris Johnson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-14 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activerecord
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "3.0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "3.0"
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: bundler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.0.0
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: jeweler
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 1.6.4
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: sqlite3-ruby
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: ruby-debug19
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
93
+ description: Create different, named representations of a resource for cleaner state representation.
94
+ email: findchris@gmail.com
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.rdoc
102
+ files:
103
+ - .document
104
+ - .rspec
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - lib/representation.rb
112
+ - lib/representation/active_record.rb
113
+ - rails/init.rb
114
+ - spec/representation_spec.rb
115
+ - spec/spec_helper.rb
116
+ - spec/support/connection.rb
117
+ - spec/support/models.rb
118
+ - spec/support/representation.sqlite3
119
+ - spec/support/schema.rb
120
+ has_rdoc: true
121
+ homepage: http://github.com/findchris/representation
122
+ licenses:
123
+ - MIT
124
+ post_install_message:
125
+ rdoc_options: []
126
+
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ hash: -3487629241149015236
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: "0"
144
+ requirements: []
145
+
146
+ rubyforge_project:
147
+ rubygems_version: 1.6.2
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: Model logical groups of attributes as different object representations.
151
+ test_files: []
152
+