attribute_cartographer 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,9 @@
1
+ *.gem
2
+ *~
3
+ .DS_Store
4
+ .bundle
5
+ .idea
6
+ Gemfile.lock
7
+ doc
8
+ pkg/*
9
+ tags
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2-p136@attribute_cartography --create
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Kris Hicks
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
data/README ADDED
@@ -0,0 +1,38 @@
1
+ = Attribute Cartographer
2
+
3
+ * http://github.com/krishicks/attribute-cartographer
4
+
5
+ == DESCRIPTION
6
+
7
+ Attribute Cartographer allows you to map an attributes hash into similarly or differently named methods, using an optional block to map the values as well.
8
+
9
+ == INSTALL
10
+
11
+ Add attribute-cartography to your Gemfile
12
+
13
+ gem 'attribute-cartographer'
14
+
15
+ Then run:
16
+
17
+ $ bundle
18
+
19
+ == USAGE
20
+ class Mapper
21
+ include AttributeCartographer
22
+
23
+ map :a, :b
24
+ map :c, :d, ->(v) { v.downcase }
25
+ end
26
+
27
+ account = Mapper.new a: 123, c: "Mapper"
28
+ account.b # => 123
29
+ account.d # => "Mapper"
30
+ account.original_attributes # => { a: 123, c: "Mapper" }
31
+
32
+ == REQUIREMENTS
33
+
34
+ * Ruby 1.9.x
35
+
36
+ == LICENSE
37
+
38
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rake'
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+
5
+ require 'bundler'
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ task :default => :spec
9
+
10
+ desc "Run all specs in spec directory (excluding plugin specs)"
11
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "attribute_cartographer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "attribute_cartographer"
7
+ s.version = AttributeCartographer::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Kris Hicks"]
10
+ s.email = ["krishicks@gmail.com"]
11
+ s.homepage = "https://github.com/krishicks/attribute-cartographer"
12
+ s.summary = %q{Map an attributes hash to methods on Ruby object while transforming the values to suit.}
13
+ s.description = %q{AttributeCartographer allows you to map an attributes hash into similarly or differently named methods, using an optional block to map the values as well.}
14
+
15
+ s.add_development_dependency('rspec', '>= 2.5')
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
22
+
@@ -0,0 +1,49 @@
1
+ module AttributeCartographer
2
+ class InvalidBlockArityError < StandardError; end
3
+ class InvalidArgumentError < StandardError; end
4
+
5
+ class << self
6
+ def included base
7
+ base.send :extend, AttributeCartographer::ClassMethods
8
+ base.send :include, AttributeCartographer::InstanceMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def map *args
14
+ @mapper ||= {}
15
+ block = (Proc === args.last) ? args.pop : ->(v) { v }
16
+
17
+ raise AttributeCartographer::InvalidArgumentError if block.arity > 1
18
+
19
+ if Array === args.first
20
+ raise AttributeCartographer::InvalidArgumentError if args.first.empty?
21
+ args.first.each { |arg| @mapper.merge! arg => [arg, block] }
22
+ elsif args.size == 2
23
+ from, to = args
24
+ @mapper.merge! from => [to, block]
25
+ elsif args.size == 1
26
+ from = args.pop
27
+ @mapper.merge! from => [from, block]
28
+ end
29
+ end
30
+ end
31
+
32
+ module InstanceMethods
33
+ def initialize attributes
34
+ @_original_attributes = attributes
35
+ mapper = self.class.instance_variable_get(:@mapper)
36
+
37
+ mapper.each { |from, (meth, block)|
38
+ value = attributes.has_key?(from) ? block.call(attributes[from]) : nil
39
+ self.send :define_singleton_method, meth, ->{ value }
40
+ } if mapper
41
+
42
+ super
43
+ end
44
+
45
+ def original_attributes
46
+ @_original_attributes
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module AttributeCartographer
2
+ VERSION = "0.0.1" unless defined?(::AttributeCartographer::VERSION)
3
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe AttributeCartographer do
4
+ after(:each) do
5
+ TestClass.instance_variable_set :@mapper, nil
6
+ end
7
+
8
+ let(:klass) {
9
+ class TestClass
10
+ include AttributeCartographer
11
+ end
12
+ }
13
+
14
+ describe "#initialize" do
15
+ context "with nothing mapped" do
16
+ it "does not try to map anything when map was not called" do
17
+ lambda { klass.new(a: :b) }.should_not raise_error
18
+ end
19
+ end
20
+
21
+ context "with attributes that don't match mapped values" do
22
+ before { klass.map :a, :b, ->(v) { v + 1 } }
23
+
24
+ it "maps attributes to nil when no mappable attribute was passed in" do
25
+ klass.new(c: :d).b.should be_nil
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "#original_attributes" do
31
+ it "returns any attributes given to initialize" do
32
+ klass.new(a: :b).original_attributes.should == { a: :b }
33
+ end
34
+ end
35
+
36
+ describe ".map" do
37
+ # tests about accepting strings or keys as both args
38
+ context "with a single argument given" do
39
+ before { klass.map :a }
40
+
41
+ it "creates an instance method matching the key name" do
42
+ klass.new(:a => :a_value).a.should == :a_value
43
+ end
44
+ end
45
+
46
+ context "with an empty array" do
47
+ subject { klass.map [] }
48
+
49
+ it "should raise an error" do
50
+ lambda { klass.map [] }.should raise_error(AttributeCartographer::InvalidArgumentError)
51
+ end
52
+ end
53
+
54
+ context "with a non-empty array" do
55
+ before { klass.map [:a, :b] }
56
+
57
+ it "creates a method named for each key" do
58
+ instance = klass.new(a: :a_value, b: :b_value)
59
+ instance.a.should == :a_value
60
+ instance.b.should == :b_value
61
+ end
62
+
63
+ it "makes nil methods for mapped keys which had no attributes passed in for them" do
64
+ instance = klass.new(a: :a_value)
65
+ instance.b.should == nil
66
+ end
67
+ end
68
+
69
+ context "with two keys and a 1-arity block given" do
70
+ before { klass.map :a, :b, ->(v) { v + 1 } }
71
+
72
+ it "creates a method named for the second key with the result of passing the associated value to the block" do
73
+ klass.new(:a => 1).b.should == 2
74
+ end
75
+ end
76
+
77
+ context "with two keys and a >1-arity block given" do
78
+ it "raises an error" do
79
+ lambda { klass.map :a, :b, ->(k,v) { v + 1 } }.should raise_error(AttributeCartographer::InvalidArgumentError)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1 @@
1
+ require "attribute_cartographer"
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_cartographer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kris Hicks
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2152785160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152785160
25
+ description: AttributeCartographer allows you to map an attributes hash into similarly
26
+ or differently named methods, using an optional block to map the values as well.
27
+ email:
28
+ - krishicks@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .rspec
35
+ - .rvmrc
36
+ - Gemfile
37
+ - LICENSE
38
+ - README
39
+ - Rakefile
40
+ - attribute_cartographer.gemspec
41
+ - lib/attribute_cartographer.rb
42
+ - lib/attribute_cartographer/VERSION.rb
43
+ - spec/attribute_cartographer_spec.rb
44
+ - spec/spec_helper.rb
45
+ homepage: https://github.com/krishicks/attribute-cartographer
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.7.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Map an attributes hash to methods on Ruby object while transforming the values
69
+ to suit.
70
+ test_files:
71
+ - spec/attribute_cartographer_spec.rb
72
+ - spec/spec_helper.rb