mm-multi-parameter-attributes 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +32 -0
- data/Rakefile +75 -0
- data/lib/mm-multi-parameter-attributes.rb +95 -0
- metadata +98 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Richard Livsey
|
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,32 @@
|
|
1
|
+
= MongoMapper::Plugins::MultiParameterAttributes
|
2
|
+
|
3
|
+
Tiny plugin for MongoMapper to add multi-parameter attributes
|
4
|
+
|
5
|
+
Based on Rails & gist by @bitzesty found at:
|
6
|
+
https://gist.github.com/268948/c5ca7d8fb02470c5c8a0a8225390a4186349a8d4
|
7
|
+
|
8
|
+
== Usage
|
9
|
+
|
10
|
+
Either load it into all models, or individual models:
|
11
|
+
|
12
|
+
# add to all models
|
13
|
+
MongoMapper::Document.append_inclusions(MongoMapper::Plugins::MultiParameterAttributes)
|
14
|
+
|
15
|
+
# add to a specific model
|
16
|
+
plugin MongoMapper::Plugins::MultiParameterAttributes
|
17
|
+
|
18
|
+
== Note on Patches/Pull Requests
|
19
|
+
|
20
|
+
* Fork the project.
|
21
|
+
* Make your feature addition or bug fix.
|
22
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
23
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
|
24
|
+
* Send me a pull request. Bonus points for topic branches.
|
25
|
+
|
26
|
+
== Install
|
27
|
+
|
28
|
+
$ gem install mm-multi-parameter-attributes
|
29
|
+
|
30
|
+
== Copyright
|
31
|
+
|
32
|
+
See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake/gempackagetask"
|
3
|
+
require "rake/rdoctask"
|
4
|
+
|
5
|
+
require "spec"
|
6
|
+
require "spec/rake/spectask"
|
7
|
+
Spec::Rake::SpecTask.new do |t|
|
8
|
+
t.spec_opts = %w(--format specdoc --colour)
|
9
|
+
t.libs = ["spec"]
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
task :default => ["spec"]
|
14
|
+
|
15
|
+
# This builds the actual gem. For details of what all these options
|
16
|
+
# mean, and other ones you can add, check the documentation here:
|
17
|
+
#
|
18
|
+
# http://rubygems.org/read/chapter/20
|
19
|
+
#
|
20
|
+
spec = Gem::Specification.new do |s|
|
21
|
+
|
22
|
+
# Change these as appropriate
|
23
|
+
s.name = "mm-multi-parameter-attributes"
|
24
|
+
s.version = "0.1.0"
|
25
|
+
s.summary = "Tiny plugin for MongoMapper to add multi-parameter attributes"
|
26
|
+
s.author = "Richard Livsey"
|
27
|
+
s.email = "richard@livsey.org"
|
28
|
+
s.homepage = "http://github.com/rlivsey/mm-multi-parameter-attributes"
|
29
|
+
|
30
|
+
s.has_rdoc = true
|
31
|
+
s.extra_rdoc_files = %w(README.rdoc)
|
32
|
+
s.rdoc_options = %w(--main README.rdoc)
|
33
|
+
|
34
|
+
# Add any extra files to include in the gem
|
35
|
+
s.files = %w(LICENSE Rakefile README.rdoc) + Dir.glob("{spec,lib/**/*}")
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
|
38
|
+
# If you want to depend on other gems, add them here, along with any
|
39
|
+
# relevant versions
|
40
|
+
s.add_dependency("mongo_mapper")
|
41
|
+
|
42
|
+
# If your tests use any gems, include them here
|
43
|
+
s.add_development_dependency("rspec")
|
44
|
+
end
|
45
|
+
|
46
|
+
# This task actually builds the gem. We also regenerate a static
|
47
|
+
# .gemspec file, which is useful if something (i.e. GitHub) will
|
48
|
+
# be automatically building a gem for this project. If you're not
|
49
|
+
# using GitHub, edit as appropriate.
|
50
|
+
#
|
51
|
+
# To publish your gem online, install the 'gemcutter' gem; Read more
|
52
|
+
# about that here: http://gemcutter.org/pages/gem_docs
|
53
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
54
|
+
pkg.gem_spec = spec
|
55
|
+
end
|
56
|
+
|
57
|
+
desc "Build the gemspec file #{spec.name}.gemspec"
|
58
|
+
task :gemspec do
|
59
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
60
|
+
File.open(file, "w") {|f| f << spec.to_ruby }
|
61
|
+
end
|
62
|
+
|
63
|
+
task :package => :gemspec
|
64
|
+
|
65
|
+
# Generate documentation
|
66
|
+
Rake::RDocTask.new do |rd|
|
67
|
+
rd.main = "README.rdoc"
|
68
|
+
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
69
|
+
rd.rdoc_dir = "rdoc"
|
70
|
+
end
|
71
|
+
|
72
|
+
desc 'Clear out RDoc and generated packages'
|
73
|
+
task :clean => [:clobber_rdoc, :clobber_package] do
|
74
|
+
rm "#{spec.name}.gemspec"
|
75
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
|
3
|
+
module MongoMapper
|
4
|
+
module Plugins
|
5
|
+
module MultiParameterAttributes
|
6
|
+
def self.included(model)
|
7
|
+
model.plugin self
|
8
|
+
end
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
def attributes=(new_attributes)
|
12
|
+
return if new_attributes.nil?
|
13
|
+
|
14
|
+
multi_parameter_attributes = []
|
15
|
+
normal_attributes = {}
|
16
|
+
|
17
|
+
new_attributes.each do |k, v|
|
18
|
+
if k.include?("(")
|
19
|
+
multi_parameter_attributes << [ k, v ]
|
20
|
+
else
|
21
|
+
normal_attributes[k] = v
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
assign_multiparameter_attributes(multi_parameter_attributes)
|
26
|
+
|
27
|
+
super(normal_attributes)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done
|
31
|
+
# by calling new on the column type or aggregation type (through composed_of) object with these parameters.
|
32
|
+
# So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate
|
33
|
+
# written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the
|
34
|
+
# parentheses to have the parameters typecasted before they're used in the constructor. Use i for Fixnum, f for Float,
|
35
|
+
# s for String, and a for Array. If all the values for a given attribute are empty, the attribute will be set to nil.
|
36
|
+
def assign_multiparameter_attributes(pairs)
|
37
|
+
execute_callstack_for_multiparameter_attributes(
|
38
|
+
extract_callstack_for_multiparameter_attributes(pairs)
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
def execute_callstack_for_multiparameter_attributes(callstack)
|
43
|
+
callstack.each do |name, values_with_empty_parameters|
|
44
|
+
# in order to allow a date to be set without a year, we must keep the empty values.
|
45
|
+
# Otherwise, we wouldn't be able to distinguish it from a date with an empty day.
|
46
|
+
values = values_with_empty_parameters.reject(&:nil?)
|
47
|
+
|
48
|
+
if !values.reject{|x| x.blank? }.empty?
|
49
|
+
key = self.class.keys[name]
|
50
|
+
raise ArgumentError, "Unknown key #{name}" if key.nil?
|
51
|
+
klass = key.type
|
52
|
+
|
53
|
+
value = if Time == klass
|
54
|
+
Time.zone.local(*values.map(&:to_i))
|
55
|
+
elsif Date == klass
|
56
|
+
begin
|
57
|
+
values = values_with_empty_parameters.map{|v| v.blank? ? 1 : v.to_i}
|
58
|
+
Date.new(*values)
|
59
|
+
rescue ArgumentError => ex # if Date.new raises an exception on an invalid date
|
60
|
+
Time.zone.local(*values.map(&:to_i)).to_date # we instantiate Time object and convert it back to a date thus using Time's logic in handling invalid dates
|
61
|
+
end
|
62
|
+
else
|
63
|
+
klass.new(*values)
|
64
|
+
end
|
65
|
+
writer_method = "#{name}="
|
66
|
+
if respond_to?(writer_method)
|
67
|
+
self.send(writer_method, value)
|
68
|
+
else
|
69
|
+
self[name.to_s] = value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def extract_callstack_for_multiparameter_attributes(pairs)
|
76
|
+
attributes = { }
|
77
|
+
|
78
|
+
for pair in pairs
|
79
|
+
multiparameter_name, value = pair
|
80
|
+
attribute_name = multiparameter_name.split("(").first
|
81
|
+
attributes[attribute_name] = [] unless attributes.include?(attribute_name)
|
82
|
+
|
83
|
+
attributes[attribute_name] << [ find_parameter_position(multiparameter_name), value ]
|
84
|
+
end
|
85
|
+
|
86
|
+
attributes.each { |name, values| attributes[name] = values.sort_by{ |v| v.first }.collect { |v| v.last } }
|
87
|
+
end
|
88
|
+
|
89
|
+
def find_parameter_position(multiparameter_name)
|
90
|
+
multiparameter_name.scan(/\(([0-9]*).*\)/).first.first
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mm-multi-parameter-attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Richard Livsey
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-17 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: mongo_mapper
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
description:
|
50
|
+
email: richard@livsey.org
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- README.rdoc
|
57
|
+
files:
|
58
|
+
- LICENSE
|
59
|
+
- Rakefile
|
60
|
+
- README.rdoc
|
61
|
+
- lib/mm-multi-parameter-attributes.rb
|
62
|
+
has_rdoc: true
|
63
|
+
homepage: http://github.com/rlivsey/mm-multi-parameter-attributes
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options:
|
68
|
+
- --main
|
69
|
+
- README.rdoc
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.7
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Tiny plugin for MongoMapper to add multi-parameter attributes
|
97
|
+
test_files: []
|
98
|
+
|