assign_by_parts 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.gitignore +5 -0
- data/.rvmrc +2 -0
- data/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +2 -0
- data/assign_by_parts.gemspec +28 -0
- data/lib/assign_by_parts.rb +2 -0
- data/lib/assign_by_parts/base.rb +76 -0
- data/lib/assign_by_parts/version.rb +3 -0
- data/lib/rails/assign_by_parts.rb +7 -0
- data/spec/assign_by_parts_spec.rb +115 -0
- data/spec/spec_helper.rb +10 -0
- metadata +124 -0
data/.gemtest
ADDED
File without changes
|
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 John Long
|
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
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
AssignByParts
|
2
|
+
============
|
3
|
+
|
4
|
+
Usage
|
5
|
+
-----
|
6
|
+
|
7
|
+
Just a way to clean up our normal way of having multiple input fields for phone numbers,
|
8
|
+
or social security numbers.
|
9
|
+
|
10
|
+
assign_by_parts :social_security_number, :area => [0, 2],
|
11
|
+
:group => [3..4],
|
12
|
+
:serial => [5..8]
|
13
|
+
|
14
|
+
|
15
|
+
In your views you would have something like:
|
16
|
+
|
17
|
+
= form.input :social_security_number_area
|
18
|
+
= form.input :social_security_number_group
|
19
|
+
= form.input :social_security_number_serial
|
20
|
+
|
21
|
+
Installation
|
22
|
+
------------
|
23
|
+
|
24
|
+
To install into a Rails 3 app just add this to your `Gemfile`:
|
25
|
+
|
26
|
+
gem "assign_by_parts"
|
27
|
+
|
28
|
+
Enjoy!
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "assign_by_parts/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "assign_by_parts"
|
7
|
+
s.version = AssignByParts::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["John Long"]
|
10
|
+
s.email = ["asceth@gmail.com"]
|
11
|
+
s.homepage = "http://github.com/asceth/assign_by_parts"
|
12
|
+
s.summary = "Making partial attribute assigning easier"
|
13
|
+
s.description = "A gem for making fields that need partial value assignments easier"
|
14
|
+
|
15
|
+
s.rubyforge_project = "assign_by_parts"
|
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
|
+
|
22
|
+
s.add_dependency "activesupport"
|
23
|
+
s.add_dependency "i18n"
|
24
|
+
|
25
|
+
s.add_development_dependency "rspec", "~>2.0.0"
|
26
|
+
s.add_development_dependency "rr"
|
27
|
+
s.add_development_dependency "rails", "~>3.0.0"
|
28
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module AssignByParts
|
2
|
+
|
3
|
+
def self.included base
|
4
|
+
require 'active_support/core_ext/string' # lazy load
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module PrivateClassMethods
|
9
|
+
attr_accessor :initial_value
|
10
|
+
end
|
11
|
+
extend PrivateClassMethods
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def assign_by_parts(*args)
|
15
|
+
case args.length
|
16
|
+
when 1 then
|
17
|
+
args.first.each {|attribute, part_definitions| setup_assign_by_parts(attribute, part_definitions)}
|
18
|
+
when 2 then
|
19
|
+
setup_assign_by_parts *args
|
20
|
+
else raise("Error in assign_by_parts syntax, expecting attribute and part definitions or a hash, got #{args.inspect}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def setup_assign_by_parts(attribute, part_definitions)
|
27
|
+
sorted_part_definitions = part_definitions.to_a.sort_by do |(part_key, (beginning_of_part, end_of_part))|
|
28
|
+
beginning_of_part
|
29
|
+
end
|
30
|
+
sorted_part_definition_keys = sorted_part_definitions.map(&:first)
|
31
|
+
|
32
|
+
class_eval <<-EOS, __FILE__, (__LINE__+1)
|
33
|
+
def #{attribute}_part_definitions
|
34
|
+
#{sorted_part_definitions.inspect}
|
35
|
+
end
|
36
|
+
|
37
|
+
def update_#{attribute}(parts)
|
38
|
+
new_attribute = new_#{attribute}_from_parts(merge_#{attribute}_parts(parts))
|
39
|
+
if defined?(ActiveRecord) && self.is_a?(ActiveRecord::Base)
|
40
|
+
#{attribute}_will_change! unless changed_attributes.include?('#{attribute}')
|
41
|
+
self[:#{attribute}] = new_attribute
|
42
|
+
else
|
43
|
+
@#{attribute} = new_attribute
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def new_#{attribute}_from_parts(parts)
|
48
|
+
#{attribute}_part_definitions.inject("") do |value, part_definition|
|
49
|
+
part_value = parts[part_definition.first]
|
50
|
+
part_definition_size = part_definition.last.last - part_definition.last.first + 1
|
51
|
+
value + (("%" + (part_definition_size).to_s + "s") % part_value[0..part_definition_size])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def merge_#{attribute}_parts(parts)
|
56
|
+
#{attribute}_part_definitions.inject({}) do |combination, (part_key, range)|
|
57
|
+
combination[part_key] = parts[part_key] || send(("#{attribute}_" + part_key.to_s).to_sym)
|
58
|
+
combination
|
59
|
+
end
|
60
|
+
end
|
61
|
+
EOS
|
62
|
+
|
63
|
+
sorted_part_definitions.each do |(part_key, (beginning_of_part, end_of_part))|
|
64
|
+
class_eval <<-EOS, __FILE__, (__LINE__+1)
|
65
|
+
def #{attribute}_#{part_key}
|
66
|
+
#{attribute}.blank? ? "" : #{attribute}[#{beginning_of_part}..#{end_of_part}]
|
67
|
+
end
|
68
|
+
|
69
|
+
def #{attribute}_#{part_key}=(value)
|
70
|
+
update_#{attribute}(:#{part_key} => value)
|
71
|
+
end
|
72
|
+
EOS
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe AssignByParts do
|
4
|
+
|
5
|
+
describe "given a ruby object" do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
class Bar
|
9
|
+
attr_accessor :social_security_number, :foo
|
10
|
+
|
11
|
+
include AssignByParts
|
12
|
+
assign_by_parts :social_security_number => {:area => [0, 2],
|
13
|
+
:group => [3, 4],
|
14
|
+
:serial => [5, 8]},
|
15
|
+
:foo => {:fum => [0, 1],
|
16
|
+
:faz => [2, 4]}
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
@bar = Bar.new
|
21
|
+
@bar.social_security_number = "000000000"
|
22
|
+
@bar.foo = "FMFAZ"
|
23
|
+
|
24
|
+
# TODO
|
25
|
+
#stub(@bar).changed_attributes { [] }
|
26
|
+
#stub(@bar).foo_will_change! { true }
|
27
|
+
#stub(@bar).social_security_number_will_change! { true }
|
28
|
+
end
|
29
|
+
|
30
|
+
context "with an attr_accessor" do
|
31
|
+
it "should call setup_assign_by_parts for each field" do
|
32
|
+
@bar.should respond_to(:social_security_number_area)
|
33
|
+
@bar.should respond_to(:social_security_number_group)
|
34
|
+
@bar.should respond_to(:social_security_number_serial)
|
35
|
+
|
36
|
+
@bar.should respond_to(:foo_fum)
|
37
|
+
@bar.should respond_to(:foo_faz)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should set a part of the attribute" do
|
41
|
+
@bar.foo_fum = "NO"
|
42
|
+
@bar.foo.should == "NOFAZ"
|
43
|
+
|
44
|
+
@bar.foo_faz = "BAR"
|
45
|
+
@bar.foo.should == "NOBAR"
|
46
|
+
|
47
|
+
@bar.social_security_number_group = "99"
|
48
|
+
@bar.social_security_number.should == "000990000"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
describe "given an active record object" do
|
55
|
+
before(:all) do
|
56
|
+
require 'active_record'
|
57
|
+
end
|
58
|
+
|
59
|
+
before(:each) do
|
60
|
+
class Baz < ActiveRecord::Base
|
61
|
+
class_inheritable_accessor :columns
|
62
|
+
|
63
|
+
def self.columns
|
64
|
+
@columns ||= []
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
68
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
69
|
+
end
|
70
|
+
|
71
|
+
column :id, :integer
|
72
|
+
column :social_security_number, :string
|
73
|
+
column :foo, :string
|
74
|
+
|
75
|
+
include AssignByParts
|
76
|
+
assign_by_parts :social_security_number => {:area => [0, 2],
|
77
|
+
:group => [3, 4],
|
78
|
+
:serial => [5, 8]},
|
79
|
+
:foo => {:fum => [0, 1],
|
80
|
+
:faz => [2, 4]}
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
@baz = Baz.new
|
85
|
+
@baz.social_security_number = "000000000"
|
86
|
+
@baz.foo = "FMFAZ"
|
87
|
+
|
88
|
+
stub(@baz).changed_attributes { [] }
|
89
|
+
stub(@baz).foo_will_change! { true }
|
90
|
+
stub(@baz).social_security_number_will_change! { true }
|
91
|
+
end
|
92
|
+
|
93
|
+
context "with an attribute" do
|
94
|
+
it "should call setup_assign_by_parts for each field" do
|
95
|
+
@baz.should respond_to(:social_security_number_area)
|
96
|
+
@baz.should respond_to(:social_security_number_group)
|
97
|
+
@baz.should respond_to(:social_security_number_serial)
|
98
|
+
|
99
|
+
@baz.should respond_to(:foo_fum)
|
100
|
+
@baz.should respond_to(:foo_faz)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should set a part of the attribute" do
|
104
|
+
@baz.foo_fum = "NO"
|
105
|
+
@baz.foo.should == "NOFAZ"
|
106
|
+
|
107
|
+
@baz.foo_faz = "BAZ"
|
108
|
+
@baz.foo.should == "NOBAZ"
|
109
|
+
|
110
|
+
@baz.social_security_number_group = "99"
|
111
|
+
@baz.social_security_number.should == "000990000"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: assign_by_parts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John Long
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-22 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activesupport
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: i18n
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.0.0
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rr
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rails
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id005
|
71
|
+
description: A gem for making fields that need partial value assignments easier
|
72
|
+
email:
|
73
|
+
- asceth@gmail.com
|
74
|
+
executables: []
|
75
|
+
|
76
|
+
extensions: []
|
77
|
+
|
78
|
+
extra_rdoc_files: []
|
79
|
+
|
80
|
+
files:
|
81
|
+
- .gemtest
|
82
|
+
- .gitignore
|
83
|
+
- .rvmrc
|
84
|
+
- Gemfile
|
85
|
+
- LICENSE
|
86
|
+
- README.md
|
87
|
+
- Rakefile
|
88
|
+
- assign_by_parts.gemspec
|
89
|
+
- lib/assign_by_parts.rb
|
90
|
+
- lib/assign_by_parts/base.rb
|
91
|
+
- lib/assign_by_parts/version.rb
|
92
|
+
- lib/rails/assign_by_parts.rb
|
93
|
+
- spec/assign_by_parts_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
has_rdoc: true
|
96
|
+
homepage: http://github.com/asceth/assign_by_parts
|
97
|
+
licenses: []
|
98
|
+
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: "0"
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: "0"
|
116
|
+
requirements: []
|
117
|
+
|
118
|
+
rubyforge_project: assign_by_parts
|
119
|
+
rubygems_version: 1.5.2
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: Making partial attribute assigning easier
|
123
|
+
test_files: []
|
124
|
+
|