mm-multi-parameter-attributes 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,7 @@ https://gist.github.com/268948/c5ca7d8fb02470c5c8a0a8225390a4186349a8d4
10
10
  Either load it into all models, or individual models:
11
11
 
12
12
  # add to all models
13
- MongoMapper::Document.append_inclusions(MongoMapper::Plugins::MultiParameterAttributes)
13
+ MongoMapper::Document.plugin(MongoMapper::Plugins::MultiParameterAttributes)
14
14
 
15
15
  # add to a specific model
16
16
  plugin MongoMapper::Plugins::MultiParameterAttributes
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ spec = Gem::Specification.new do |s|
15
15
 
16
16
  # Change these as appropriate
17
17
  s.name = "mm-multi-parameter-attributes"
18
- s.version = "0.2.1"
18
+ s.version = "0.2.2"
19
19
  s.summary = "Tiny plugin for MongoMapper to add multi-parameter attributes"
20
20
  s.author = "Richard Livsey"
21
21
  s.email = "richard@livsey.org"
@@ -32,6 +32,7 @@ spec = Gem::Specification.new do |s|
32
32
  # If you want to depend on other gems, add them here, along with any
33
33
  # relevant versions
34
34
  s.add_dependency("mongo_mapper", ">= 0.9.0")
35
+ s.add_dependency("tzinfo")
35
36
 
36
37
  # If your tests use any gems, include them here
37
38
  s.add_development_dependency("rspec")
@@ -42,7 +43,7 @@ end
42
43
  # be automatically building a gem for this project. If you're not
43
44
  # using GitHub, edit as appropriate.
44
45
  #
45
- # To publish your gem online, install the 'gemcutter' gem; Read more
46
+ # To publish your gem online, install the 'gemcutter' gem; Read more
46
47
  # about that here: http://gemcutter.org/pages/gem_docs
47
48
  Rake::GemPackageTask.new(spec) do |pkg|
48
49
  pkg.gem_spec = spec
@@ -1,95 +1,104 @@
1
1
  require 'mongo_mapper'
2
+ require 'tzinfo'
2
3
 
3
4
  module MongoMapper
4
5
  module Plugins
5
6
  module MultiParameterAttributes
6
7
  extend ActiveSupport::Concern
7
8
 
8
- module InstanceMethods
9
- def attributes=(new_attributes)
10
- return if new_attributes.nil?
9
+ def attributes=(new_attributes)
10
+ return if new_attributes.nil?
11
11
 
12
- multi_parameter_attributes = []
13
- normal_attributes = {}
12
+ multi_parameter_attributes = []
13
+ normal_attributes = {}
14
14
 
15
- new_attributes.each do |k, v|
16
- if k.to_s.include?("(")
17
- multi_parameter_attributes << [ k.to_s, v ]
18
- else
19
- normal_attributes[k] = v
20
- end
15
+ new_attributes.each do |k, v|
16
+ if k.to_s.include?("(")
17
+ multi_parameter_attributes << [ k.to_s, v ]
18
+ else
19
+ normal_attributes[k] = v
21
20
  end
21
+ end
22
22
 
23
- assign_multiparameter_attributes(multi_parameter_attributes)
23
+ assign_multiparameter_attributes(multi_parameter_attributes)
24
24
 
25
- super(normal_attributes)
26
- end
25
+ super(normal_attributes)
26
+ end
27
27
 
28
- # Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done
29
- # by calling new on the column type or aggregation type (through composed_of) object with these parameters.
30
- # So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate
31
- # written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the
32
- # parentheses to have the parameters typecasted before they're used in the constructor. Use i for Fixnum, f for Float,
33
- # s for String, and a for Array. If all the values for a given attribute are empty, the attribute will be set to nil.
34
- def assign_multiparameter_attributes(pairs)
35
- execute_callstack_for_multiparameter_attributes(
36
- extract_callstack_for_multiparameter_attributes(pairs)
37
- )
38
- end
28
+ # Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done
29
+ # by calling new on the column type or aggregation type (through composed_of) object with these parameters.
30
+ # So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate
31
+ # written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the
32
+ # parentheses to have the parameters typecasted before they're used in the constructor. Use i for Fixnum, f for Float,
33
+ # s for String, and a for Array. If all the values for a given attribute are empty, the attribute will be set to nil.
34
+ def assign_multiparameter_attributes(pairs)
35
+ execute_callstack_for_multiparameter_attributes(
36
+ extract_callstack_for_multiparameter_attributes(pairs)
37
+ )
38
+ end
39
39
 
40
- def execute_callstack_for_multiparameter_attributes(callstack)
41
- callstack.each do |name, values_with_empty_parameters|
42
- # in order to allow a date to be set without a year, we must keep the empty values.
43
- # Otherwise, we wouldn't be able to distinguish it from a date with an empty day.
44
- values = values_with_empty_parameters.reject(&:nil?)
45
-
46
- if !values.reject{|x| x.blank? }.empty?
47
- values = values.map(&:to_i)
48
-
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)
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).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
40
+ def execute_callstack_for_multiparameter_attributes(callstack)
41
+ callstack.each do |name, values_with_empty_parameters|
42
+ # in order to allow a date to be set without a year, we must keep the empty values.
43
+ # Otherwise, we wouldn't be able to distinguish it from a date with an empty day.
44
+ values = values_with_empty_parameters.reject(&:blank?)
45
+
46
+ if values.any?
47
+
48
+ key = self.class.keys[name]
49
+ raise ArgumentError, "Unknown key #{name}" if key.nil?
50
+ klass = key.type
51
+
52
+ value = if Time == klass
53
+ Time.zone.local(*valid_datetime_values(values_with_empty_parameters))
54
+ elsif Date == klass
55
+ values = valid_datetime_values(values_with_empty_parameters)
56
+ begin
57
+ Date.new(*values)
58
+ rescue ArgumentError => ex # if Date.new raises an exception on an invalid date
59
+ Time.zone.local(*values).to_date # we instantiate Time object and convert it back to a date thus using Time's logic in handling invalid dates
60
+ end
61
+ else
62
+ klass.new(*values_with_empty_parameters)
63
+ end
64
+ else
65
+ value = nil
66
+ end
67
+ writer_method = "#{name}="
68
+ if respond_to?(writer_method)
69
+ self.send(writer_method, value)
70
+ else
71
+ self[name.to_s] = value
72
72
  end
73
73
  end
74
+ end
74
75
 
75
- def extract_callstack_for_multiparameter_attributes(pairs)
76
- attributes = { }
76
+ # Ensures that values for date are set to now if blank (as month and say cannot be 0) and that all values are converted to integers
77
+ def valid_datetime_values(values)
78
+ now = Time.zone.now
79
+ values[0] = now.year if values[0].blank?
80
+ values[1] = now.month if values[1].blank?
81
+ values[2] = now.day if values[2].blank?
82
+ values.map(&:to_i)
83
+ end
77
84
 
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)
85
+ def extract_callstack_for_multiparameter_attributes(pairs)
86
+ attributes = { }
82
87
 
83
- attributes[attribute_name] << [ find_parameter_position(multiparameter_name), value ]
84
- end
88
+ for pair in pairs
89
+ multiparameter_name, value = pair
90
+ attribute_name = multiparameter_name.split("(").first
91
+ attributes[attribute_name] = [] unless attributes.include?(attribute_name)
85
92
 
86
- attributes.each { |name, values| attributes[name] = values.sort_by{ |v| v.first }.collect { |v| v.last } }
93
+ attributes[attribute_name] << [ find_parameter_position(multiparameter_name), value ]
87
94
  end
88
95
 
89
- def find_parameter_position(multiparameter_name)
90
- multiparameter_name.scan(/\(([0-9]*).*\)/).first.first
91
- end
96
+ attributes.each { |name, values| attributes[name] = values.sort_by{ |v| v.first }.collect { |v| v.last } }
97
+ end
98
+
99
+ def find_parameter_position(multiparameter_name)
100
+ multiparameter_name.scan(/\(([0-9]*).*\)/).first.first
92
101
  end
93
102
  end
94
103
  end
95
- end
104
+ end
metadata CHANGED
@@ -1,100 +1,87 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mm-multi-parameter-attributes
3
- version: !ruby/object:Gem::Version
4
- hash: 21
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Richard Livsey
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-03-09 00:00:00 +00:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: mongo_mapper
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70196703635800 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 59
30
- segments:
31
- - 0
32
- - 9
33
- - 0
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
34
21
  version: 0.9.0
35
22
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: rspec
39
23
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70196703635800
25
+ - !ruby/object:Gem::Dependency
26
+ name: tzinfo
27
+ requirement: &70196703635420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70196703635420
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70196703634980 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 3
46
- segments:
47
- - 0
48
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
49
44
  type: :development
50
- version_requirements: *id002
45
+ prerelease: false
46
+ version_requirements: *70196703634980
51
47
  description:
52
48
  email: richard@livsey.org
53
49
  executables: []
54
-
55
50
  extensions: []
56
-
57
- extra_rdoc_files:
51
+ extra_rdoc_files:
58
52
  - README.rdoc
59
- files:
53
+ files:
60
54
  - LICENSE
61
55
  - Rakefile
62
56
  - README.rdoc
63
57
  - lib/mm-multi-parameter-attributes.rb
64
- has_rdoc: true
65
58
  homepage: http://github.com/rlivsey/mm-multi-parameter-attributes
66
59
  licenses: []
67
-
68
60
  post_install_message:
69
- rdoc_options:
61
+ rdoc_options:
70
62
  - --main
71
63
  - README.rdoc
72
- require_paths:
64
+ require_paths:
73
65
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
66
+ required_ruby_version: !ruby/object:Gem::Requirement
75
67
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ segments:
81
73
  - 0
82
- version: "0"
83
- required_rubygems_version: !ruby/object:Gem::Requirement
74
+ hash: -305387027764767057
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
76
  none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- hash: 3
89
- segments:
90
- - 0
91
- version: "0"
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
92
81
  requirements: []
93
-
94
82
  rubyforge_project:
95
- rubygems_version: 1.4.1
83
+ rubygems_version: 1.8.17
96
84
  signing_key:
97
85
  specification_version: 3
98
86
  summary: Tiny plugin for MongoMapper to add multi-parameter attributes
99
87
  test_files: []
100
-