figurine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in figurine.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 hubert
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,29 @@
1
+ # Figurine
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'figurine'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install figurine
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/figurine.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'figurine/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "figurine"
8
+ spec.version = Figurine::VERSION
9
+ spec.authors = ["Hubert Huang", "Ryan Moran"]
10
+ spec.email = ["hubert77@gmail.com", "ryan.moran@gmail.com"]
11
+ spec.description = %q{More intuitive replacement for accepts nested attributes}
12
+ spec.summary = %q{Big things coming...}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ end
data/lib/figurine.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "figurine/version"
2
+
3
+ module Figurine
4
+ autoload :Form, 'figurine/form'
5
+ autoload :One, 'figurine/one'
6
+ autoload :Many, 'figurine/many'
7
+ end
@@ -0,0 +1,14 @@
1
+ module Figurine
2
+ class Form
3
+ extend One
4
+ extend Many
5
+
6
+ def initialize(attributes= {})
7
+ @attributes = {}
8
+
9
+ attributes.each do |key, value|
10
+ self.send("#{key}=", value)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ module Figurine
2
+ module Many
3
+ def many(model, options = {})
4
+ @whitelist ||= {}
5
+ @whitelist[model] = options[:only] || []
6
+
7
+ define_method model do
8
+ @attributes[model]
9
+ end
10
+
11
+ define_method "#{model}=" do |values|
12
+ whitelist = self.class.instance_variable_get("@whitelist")[model]
13
+ @attributes[model] = values.map do |val|
14
+ attributes = if val.respond_to?(:to_hash)
15
+ Hash[*val.to_hash.select { |key, _| whitelist.include?(key) || whitelist.empty? }.flatten]
16
+ elsif val.respond_to?(:attributes)
17
+ Hash[*val.attributes.select { |key, _| whitelist.include?(key) || whitelist.empty? }.flatten]
18
+ end
19
+ val[:id] ? attributes.merge(:id => val[:id]) : attributes
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module Figurine
2
+ module One
3
+
4
+ def one(model, attributes = {})
5
+ @whitelist ||= {}
6
+ @whitelist[model] = attributes[:only] || []
7
+
8
+ define_method model do
9
+ @attributes[model]
10
+ end
11
+
12
+ define_method "#{model}=" do |val|
13
+ whitelist = self.class.instance_variable_get("@whitelist")[model]
14
+ @attributes[model] = if val.respond_to?(:to_hash)
15
+ Hash[*val.to_hash.select { |key, _| whitelist.include?(key) || whitelist.empty? }.flatten]
16
+ elsif val.respond_to?(:attributes)
17
+ Hash[*val.attributes.select { |key, _| whitelist.include?(key) || whitelist.empty? }.flatten]
18
+ end
19
+ val[:id] ? @attributes[model].merge!(:id => val[:id]) : @attributes_model
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Figurine
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ class CarForm < Figurine::Form
4
+ many :seats, :only => [:material, :color]
5
+ many :tires
6
+ end
7
+
8
+ describe 'Figurine::Many' do
9
+ context 'many collaborator does not have whitelist' do
10
+ it 'allows many collaborators to be set via a collection of objects that respond to attributes' do
11
+ tires = [Tire.new(:wear_rating => 'QR'), Tire.new(:wear_rating => 'ZR')]
12
+ car_form = CarForm.new(:tires => tires)
13
+ expect(car_form.tires).to eql([{:wear_rating => 'QR'}, {:wear_rating => 'ZR'}])
14
+ end
15
+
16
+ it 'allows many collaborators to be set via a collection of hashes' do
17
+ car_form = CarForm.new(:tires => [{:wear_rating => 'QR'}, {:wear_rating => 'ZR'}])
18
+ expect(car_form.tires).to eql([{:wear_rating => 'QR'}, {:wear_rating => 'ZR'}])
19
+ end
20
+ end
21
+
22
+ context 'many collaborators have whitelist' do
23
+ context 'id passed' do
24
+ it 'allows many collaborators to be set with a whitelist of attributes from objects' do
25
+ seats = [
26
+ Seat.new(:material => 'Leather', :color => 'Black', :piping => 'Red', :id => 1),
27
+ Seat.new(:material => 'Cloth', :color => 'Brown', :piping => 'White', :id => 2)
28
+ ]
29
+ car_form = CarForm.new(:seats => seats)
30
+ expect(car_form.seats).to eql([
31
+ {:material => 'Leather', :color => 'Black', :id => 1},
32
+ {:material => 'Cloth', :color => 'Brown', :id => 2}
33
+ ])
34
+ end
35
+
36
+ it 'allows many collaborators to be set with a whitelist of attributes from hashes' do
37
+ seats = [
38
+ { :material => 'Leather', :color => 'Black', :piping => 'Red', :id => 1 },
39
+ { :material => 'Cloth', :color => 'Brown', :piping => 'White', :id => 2 }
40
+ ]
41
+ car_form = CarForm.new(:seats => seats)
42
+ expect(car_form.seats).to eql([
43
+ {:material => 'Leather', :color => 'Black', :id => 1},
44
+ {:material => 'Cloth', :color => 'Brown', :id => 2}
45
+ ])
46
+ end
47
+
48
+ it 'allows many collaborators to be set with a whitelist of attributes from a mixed collection' do
49
+ seats = [
50
+ Seat.new(:material => 'Leather', :color => 'Black', :piping => 'Red', :id => 1),
51
+ { :material => 'Cloth', :color => 'Brown', :piping => 'White', :id => 2 }
52
+ ]
53
+ car_form = CarForm.new(:seats => seats)
54
+ expect(car_form.seats).to eql([
55
+ {:material => 'Leather', :color => 'Black', :id => 1},
56
+ {:material => 'Cloth', :color => 'Brown', :id => 2}
57
+ ])
58
+ end
59
+ end
60
+
61
+ context 'no id provided' do
62
+ it 'allows many collaborators to be set with a whitelist of attributes from objects' do
63
+ seats = [
64
+ Seat.new(:material => 'Leather', :color => 'Black', :piping => 'Red'),
65
+ Seat.new(:material => 'Cloth', :color => 'Brown', :piping => 'White')
66
+ ]
67
+ car_form = CarForm.new(:seats => seats)
68
+ expect(car_form.seats).to eql([{:material => 'Leather', :color => 'Black'}, {:material => 'Cloth', :color => 'Brown'}])
69
+ end
70
+
71
+ it 'allows many collaborators to be set with a whitelist of attributes from hashes' do
72
+ seats = [
73
+ { :material => 'Leather', :color => 'Black', :piping => 'Red' },
74
+ { :material => 'Cloth', :color => 'Brown', :piping => 'White' }
75
+ ]
76
+ car_form = CarForm.new(:seats => seats)
77
+ expect(car_form.seats).to eql([{:material => 'Leather', :color => 'Black'}, {:material => 'Cloth', :color => 'Brown'}])
78
+ end
79
+
80
+ it 'allows many collaborators to be set with a whitelist of attributes from a mixed collection' do
81
+ seats = [
82
+ Seat.new(:material => 'Leather', :color => 'Black', :piping => 'Red'),
83
+ { :material => 'Cloth', :color => 'Brown', :piping => 'White' }
84
+ ]
85
+ car_form = CarForm.new(:seats => seats)
86
+ expect(car_form.seats).to eql([{:material => 'Leather', :color => 'Black'}, {:material => 'Cloth', :color => 'Brown'}])
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Figurine::One' do
4
+ class CarForm < Figurine::Form
5
+ one :trunk
6
+ one :engine, :only => [:horsepower, :torque]
7
+ end
8
+
9
+ describe '#new' do
10
+ it 'allows a one collaborator to be set via a hash' do
11
+ car_form = CarForm.new(:trunk => { :foo => 'bar', :baz => 'qux' })
12
+ expect(car_form.trunk).to eql(:foo => 'bar', :baz => 'qux')
13
+ end
14
+
15
+
16
+ it 'allows a one collaborator to be set via an object responding to #attributes' do
17
+ trunk = Trunk.new(:foo => 'bar', :baz => 'qux')
18
+ car_form = CarForm.new(:trunk => trunk)
19
+ expect(car_form.trunk).to eql(:foo => 'bar', :baz => 'qux')
20
+ end
21
+ end
22
+
23
+ describe 'accessors' do
24
+ it 'creates an accessor for setting the one collaborator via an object responding to #attributes' do
25
+ trunk = Trunk.new(:foo => 'bar', :baz => 'qux')
26
+ car_form = CarForm.new
27
+ car_form.trunk = trunk
28
+ expect(car_form.trunk).to eql(:foo => 'bar', :baz => 'qux')
29
+ end
30
+
31
+ it 'creates an accessor for setting the one collaborator via a hash' do
32
+ car_form = CarForm.new
33
+ car_form.trunk = { :foo => 'bar', :baz => 'qux' }
34
+ expect(car_form.trunk).to eql(:foo => 'bar', :baz => 'qux')
35
+ end
36
+ end
37
+
38
+ describe 'assigning non-attribute values' do
39
+ it 'allows the assignment of non-attribute values if accessor is defined' do
40
+ class CarForm
41
+ attr_accessor :something
42
+ end
43
+ something = mock :something
44
+ car_form = CarForm.new(:trunk => { :foo => 'bar', :baz => 'qux' }, :something => something)
45
+ expect(car_form.something).to eql(something)
46
+ end
47
+
48
+ it 'raises undefined method error if accessor is not defined' do
49
+ class CarForm
50
+ undef :something
51
+ undef :something=
52
+ end
53
+ something = mock :something
54
+ expect { CarForm.new(:trunk => { :foo => 'bar', :baz => 'qux' }, :something => something) }.to raise_error(NoMethodError)
55
+ end
56
+ end
57
+
58
+ describe 'whitelisting attrs' do
59
+ it 'only sets attrs for the specified atts' do
60
+ engine = Engine.new({ :horsepower => 800, :torque => 700, :cylinders => 16 })
61
+ car_form = CarForm.new(:engine => engine)
62
+ expect(car_form.engine).to eql(:horsepower => 800, :torque => 700)
63
+ end
64
+
65
+ it 'does not strip out id if non-nil' do
66
+ engine = Engine.new({ :horsepower => 800, :torque => 700, :cylinders => 16, :id => 1 })
67
+ car_form = CarForm.new(:engine => engine)
68
+ expect(car_form.engine).to eql(:horsepower => 800, :torque => 700, :id => 1)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,2 @@
1
+ require 'figurine'
2
+ require 'support/domain_objects'
@@ -0,0 +1,29 @@
1
+ module Hashable
2
+ def initialize(attrs)
3
+ @attrs = attrs
4
+ end
5
+
6
+ def attributes
7
+ @attrs
8
+ end
9
+
10
+ def [](key)
11
+ @attrs[key]
12
+ end
13
+ end
14
+
15
+ class Trunk
16
+ include Hashable
17
+ end
18
+
19
+ class Engine
20
+ include Hashable
21
+ end
22
+
23
+ class Tire
24
+ include Hashable
25
+ end
26
+
27
+ class Seat
28
+ include Hashable
29
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: figurine
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Hubert Huang
14
+ - Ryan Moran
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2013-05-08 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 9
30
+ segments:
31
+ - 1
32
+ - 3
33
+ version: "1.3"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ description: More intuitive replacement for accepts nested attributes
65
+ email:
66
+ - hubert77@gmail.com
67
+ - ryan.moran@gmail.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - figurine.gemspec
81
+ - lib/figurine.rb
82
+ - lib/figurine/form.rb
83
+ - lib/figurine/many.rb
84
+ - lib/figurine/one.rb
85
+ - lib/figurine/version.rb
86
+ - spec/figurine/many_spec.rb
87
+ - spec/figurine/one_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/support/domain_objects.rb
90
+ homepage: ""
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.15
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Big things coming...
123
+ test_files:
124
+ - spec/figurine/many_spec.rb
125
+ - spec/figurine/one_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/support/domain_objects.rb