motion_coercible 0.2.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODBjMjc2YjRlNzQxNWQ5MDc2YTE0MDE4NGIzNTM5ZDZjMDlmMjg3OA==
4
+ ZWI5NzdjNmUxZGVjMzA4NmNhOWUzZWNiNGY2MTRkNTZiOTYzNDNiZg==
5
5
  data.tar.gz: !binary |-
6
- ZDVjNmFjYjAyNjE1ODQzODQ4MDlkOTUzNzliODY0ZDJiNDIxMzBlZQ==
6
+ Y2NlMWY0NjRhYmVjYzhiYzFlYjU5NmIzMGI2MjA3ZjI0Y2QwYmFlNw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MzEyODc3ZjYzMGM0NTQ4MTVhNTlmZDNlYmViMWU4NTkyNjlkMjZkOTZhNmNi
10
- OGEyNWRkYjU5Y2RiOWVkODJkYjRjOTM3NTc4MjMzMTAyYTVkZTVmZDI2Nzlj
11
- OWUyNDRmNmMzNDZjZWZkNzBhZTM3YzgyN2JhYmY1NjNiYzU1YzM=
9
+ MWM3MWM4N2M2ZGIxYTE0YmVhZjUzYTJjM2JkMTlhYjgzZDVmODNkYzYzNDkw
10
+ MDY0ZDY1NWJlOGNiZGRjMTQ0ZTBhMmMxZThjYjc0MmQxNzY4ZDQ1NGJiMjhi
11
+ ZjE0ZmFiYjM5ZWJmZDgxMzVhNDA0OTQ5OGU4ZWRkZmQ3MDdkOGE=
12
12
  data.tar.gz: !binary |-
13
- ZmZkMzM4MGIzYWMzYmYyMmU2ODY1MWNhODBkYWNhNDgyY2ZhMTJmNGY5NTEw
14
- YWMwNzQzN2U2ODA5Y2JhNzIyYTEwNjBkZmVkYzY5MTQ3NTlmYTY1NzJjZTg2
15
- ZmFmZTRjMTAxM2JmODg0OGJjYzczMzc2NzZjZWE5Mzk0NTE0MTA=
13
+ M2I4OWE4ZDk0OTU2NzQwMTBiNjZjZmIxMDNiOWRiZGE5YjA5MzVmZjc0NDE0
14
+ NTNjNjQxZmExZGNiNjllOWFiNzMzM2VjZjUzYjE5ZTc4ZWE1ZDcwMDg2OGMz
15
+ NGY5OTE2ZjJlM2Q0OTBhMDk0YTI2NDEwYWMyY2E4N2I3YzliYjA=
@@ -1,3 +1,5 @@
1
+ motion_require 'coercible'
2
+
1
3
  module Coercible
2
4
 
3
5
  # Coercer object
@@ -1,3 +1,5 @@
1
+ motion_require 'object'
2
+
1
3
  module Coercible
2
4
  class Coercer
3
5
 
@@ -0,0 +1,30 @@
1
+ motion_require 'object'
2
+ motion_require 'time_coercions'
3
+
4
+ module Coercible
5
+ class Coercer
6
+
7
+ # Coerce Date values
8
+ class Date < Object
9
+ include TimeCoercions
10
+
11
+ primitive ::Date
12
+
13
+ # Passthrough the value
14
+ #
15
+ # @example
16
+ # coercer[DateTime].to_date(date) # => Date object
17
+ #
18
+ # @param [DateTime] value
19
+ #
20
+ # @return [Date]
21
+ #
22
+ # @api public
23
+ def to_date(value)
24
+ value
25
+ end
26
+
27
+ end # class Date
28
+
29
+ end # class Coercer
30
+ end # module Coercible
@@ -0,0 +1,27 @@
1
+ module Coercible
2
+ class Coercer
3
+
4
+ # Coerce DateTime values
5
+ class DateTime < Object
6
+ primitive ::DateTime
7
+
8
+ include TimeCoercions
9
+
10
+ # Passthrough the value
11
+ #
12
+ # @example
13
+ # coercer[DateTime].to_datetime(datetime) # => DateTime object
14
+ #
15
+ # @param [DateTime] value
16
+ #
17
+ # @return [Date]
18
+ #
19
+ # @api public
20
+ def to_datetime(value)
21
+ value
22
+ end
23
+
24
+ end # class DateTime
25
+
26
+ end # class Coercer
27
+ end # module Coercible
@@ -0,0 +1,43 @@
1
+ motion_require 'object'
2
+
3
+ module Coercible
4
+ class Coercer
5
+
6
+ # Coerce BigDecimal values
7
+ class Decimal < Numeric
8
+ primitive ::BigDecimal
9
+
10
+ FLOAT_FORMAT = 'F'.freeze
11
+
12
+ # Coerce given value to String
13
+ #
14
+ # @example
15
+ # coercer[BigDecimal].to_string(BigDecimal('1.0')) # => "1.0"
16
+ #
17
+ # @param [BigDecimal] value
18
+ #
19
+ # @return [String]
20
+ #
21
+ # @api public
22
+ def to_string(value)
23
+ value.to_s(FLOAT_FORMAT)
24
+ end
25
+
26
+ # Passthrough the value
27
+ #
28
+ # @example
29
+ # Coercible::Coercion::BigDecimal.to_decimal(BigDecimal('1.0')) # => BigDecimal('1.0')
30
+ #
31
+ # @param [BigDecimal] value
32
+ #
33
+ # @return [Fixnum]
34
+ #
35
+ # @api public
36
+ def to_decimal(value)
37
+ value
38
+ end
39
+
40
+ end # class BigDecimal
41
+
42
+ end # class Coercer
43
+ end # module Coercible
@@ -1,3 +1,5 @@
1
+ motion_require 'object'
2
+
1
3
  module Coercible
2
4
  class Coercer
3
5
 
@@ -1,3 +1,5 @@
1
+ motion_require 'numeric'
2
+
1
3
  module Coercible
2
4
  class Coercer
3
5
 
@@ -19,6 +21,21 @@ module Coercible
19
21
  value
20
22
  end
21
23
 
24
+ # Coerce given value to a DateTime
25
+ #
26
+ # @example
27
+ # datetime = Coercible::Coercion::Float.to_datetime(1000000000.999) # => Sun, 09 Sep 2001 01:46:40 +0000
28
+ # datetime.to_f # => 1000000000.999
29
+ #
30
+ # @param [Float] value
31
+ #
32
+ # @return [DateTime]
33
+ #
34
+ # @api public
35
+ def to_datetime(value)
36
+ ::DateTime.strptime((value * 10**3).to_s, "%Q")
37
+ end
38
+
22
39
  end # class Float
23
40
 
24
41
  end # class Coercer
@@ -1,3 +1,5 @@
1
+ motion_require 'object'
2
+
1
3
  module Coercible
2
4
  class Coercer
3
5
 
@@ -20,6 +22,32 @@ module Coercible
20
22
  ::Time.local(*extract(value))
21
23
  end
22
24
 
25
+ # Creates a Date instance from a Hash
26
+ #
27
+ # Valid keys are: :year, :month, :day, :hour
28
+ #
29
+ # @param [Hash] value
30
+ #
31
+ # @return [Date]
32
+ #
33
+ # @api private
34
+ def to_date(value)
35
+ ::Date.new(*extract(value).first(3))
36
+ end
37
+
38
+ # Creates a DateTime instance from a Hash
39
+ #
40
+ # Valid keys are: :year, :month, :day, :hour, :min, :sec
41
+ #
42
+ # @param [Hash] value
43
+ #
44
+ # @return [DateTime]
45
+ #
46
+ # @api private
47
+ def to_datetime(value)
48
+ ::DateTime.new(*extract(value))
49
+ end
50
+
23
51
  private
24
52
 
25
53
  # Extracts the given args from a Hash
@@ -1,3 +1,6 @@
1
+ motion_require 'numeric'
2
+ motion_require 'configurable'
3
+
1
4
  module Coercible
2
5
  class Coercer
3
6
 
@@ -7,7 +10,7 @@ module Coercible
7
10
 
8
11
  primitive ::Integer
9
12
 
10
- config_keys [ :boolean_map ]
13
+ config_keys [ :datetime_format, :datetime_proc, :boolean_map ]
11
14
 
12
15
  # Return default config for Integer coercer type
13
16
  #
@@ -18,10 +21,32 @@ module Coercible
18
21
  # @api private
19
22
  def self.config
20
23
  super do |config|
24
+ # FIXME: Remove after Rubinius 2.0 is released
25
+ config.datetime_format, config.datetime_proc =
26
+ if Coercible.rbx?
27
+ [ '%Q', Proc.new { |value| "#{value * 10**3}" } ]
28
+ else
29
+ [ '%s', Proc.new { |value| "#{value}" } ]
30
+ end
31
+
21
32
  config.boolean_map = { 0 => false, 1 => true }
22
33
  end
23
34
  end
24
35
 
36
+ # Return datetime format from config
37
+ #
38
+ # @return [::String]
39
+ #
40
+ # @api private
41
+ attr_reader :datetime_format
42
+
43
+ # Return datetime proc from config
44
+ #
45
+ # @return [Proc]
46
+ #
47
+ # @api private
48
+ attr_reader :datetime_proc
49
+
25
50
  # Return boolean map from config
26
51
  #
27
52
  # @return [::Hash]
@@ -37,6 +62,8 @@ module Coercible
37
62
  def initialize(coercer = Coercer.new, config = self.class.config)
38
63
  super(coercer)
39
64
  @boolean_map = config.boolean_map
65
+ @datetime_format = config.datetime_format
66
+ @datetime_proc = config.datetime_proc
40
67
  end
41
68
 
42
69
  # Coerce given value to String
@@ -86,6 +113,20 @@ module Coercible
86
113
  }
87
114
  end
88
115
 
116
+ # Coerce given value to a DateTime
117
+ #
118
+ # @example
119
+ # coercer[Integer].to_datetime(0) # => Thu, 01 Jan 1970 00:00:00 +0000
120
+ #
121
+ # @param [Integer] value
122
+ #
123
+ # @return [DateTime]
124
+ #
125
+ # @api public
126
+ def to_datetime(value)
127
+ ::DateTime.strptime(datetime_proc.call(value), datetime_format)
128
+ end
129
+
89
130
  end # class Fixnum
90
131
 
91
132
  end # class Coercer
@@ -1,3 +1,5 @@
1
+ motion_require 'object'
2
+
1
3
  module Coercible
2
4
  class Coercer
3
5
 
@@ -47,6 +49,20 @@ module Coercible
47
49
  value.to_f
48
50
  end
49
51
 
52
+ # Coerce a BigDecimal instance from a numeric object
53
+ #
54
+ # @example
55
+ # coercer[Numeric].to_decimal(Rational(2, 2)) # => BigDecimal('1.0')
56
+ #
57
+ # @param [Numeric] value
58
+ #
59
+ # @return [BigDecimal]
60
+ #
61
+ # @api public
62
+ def to_decimal(value)
63
+ to_string(value).to_d
64
+ end
65
+
50
66
  end # class Numeric
51
67
 
52
68
  end # class Coercer
@@ -1,3 +1,5 @@
1
+ motion_require '../coercer.rb'
2
+
1
3
  module Coercible
2
4
  class Coercer
3
5
 
@@ -178,7 +180,7 @@ module Coercible
178
180
  #
179
181
  # @api private
180
182
  def coerce_with_method(value, method, ref_method)
181
- value.respond_to?(method) ? value.public_send(method) : raise_unsupported_coercion(value, ref_method)
183
+ value.respond_to?(method) ? value.public_send(method) : value #raise_unsupported_coercion(value, ref_method)
182
184
  end
183
185
 
184
186
  end # class Object
@@ -1,3 +1,6 @@
1
+ motion_require 'object'
2
+ motion_require 'configurable'
3
+
1
4
  module Coercible
2
5
  class Coercer
3
6
 
@@ -96,6 +99,34 @@ module Coercible
96
99
  parse_value(::Time, value, __method__)
97
100
  end
98
101
 
102
+ # Coerce given value to Date
103
+ #
104
+ # @example
105
+ # coercer[String].to_date(string) # => Date object
106
+ #
107
+ # @param [String] value
108
+ #
109
+ # @return [Date]
110
+ #
111
+ # @api public
112
+ def to_date(value)
113
+ parse_value(::Date, value, __method__)
114
+ end
115
+
116
+ # Coerce given value to DateTime
117
+ #
118
+ # @example
119
+ # coercer[String].to_datetime(string) # => DateTime object
120
+ #
121
+ # @param [String] value
122
+ #
123
+ # @return [DateTime]
124
+ #
125
+ # @api public
126
+ def to_datetime(value)
127
+ parse_value(::DateTime, value, __method__)
128
+ end
129
+
99
130
  # Coerce value to TrueClass or FalseClass
100
131
  #
101
132
  # @example with "T"
@@ -1,3 +1,5 @@
1
+ motion_require 'object'
2
+
1
3
  module Coercible
2
4
  class Coercer
3
5
 
@@ -1,3 +1,6 @@
1
+ motion_require 'object'
2
+ motion_require 'time_coercions'
3
+
1
4
  module Coercible
2
5
  class Coercer
3
6
 
@@ -32,6 +32,34 @@ module Coercible
32
32
  coerce_with_method(value, :to_time)
33
33
  end
34
34
 
35
+ # Coerce given value to DateTime
36
+ #
37
+ # @example
38
+ # coercer[Time].to_datetime(time) # => DateTime object
39
+ #
40
+ # @param [Date,Time] value
41
+ #
42
+ # @return [DateTime]
43
+ #
44
+ # @api public
45
+ def to_datetime(value)
46
+ coerce_with_method(value, :to_datetime)
47
+ end
48
+
49
+ # Coerce given value to Date
50
+ #
51
+ # @example
52
+ # coercer[Time].to_date(time) # => Date object
53
+ #
54
+ # @param [Time,DateTime] value
55
+ #
56
+ # @return [Date]
57
+ #
58
+ # @api public
59
+ def to_date(value)
60
+ coerce_with_method(value, :to_date)
61
+ end
62
+
35
63
  private
36
64
 
37
65
  # Try to use native coercion method on the given value
@@ -1,3 +1,5 @@
1
+ motion_require 'object'
2
+
1
3
  module Coercible
2
4
  class Coercer
3
5
 
@@ -1,3 +1,7 @@
1
+ motion_require 'stubs.rb'
2
+ motion_require 'support/type_lookup'
3
+ motion_require 'support/options'
4
+
1
5
  module Coercible
2
6
  EXTRA_CONST_ARGS = (RUBY_VERSION < '1.9' ? [] : [ false ]).freeze
3
7
 
@@ -16,7 +16,7 @@ module Coercible
16
16
  keys.each do |key|
17
17
  config.instance_eval do
18
18
  define_singleton_method("#{key}") { instance_variable_get("@#{key}") }
19
- define_singleton_method("#{key}=") {|value| instance_variable_set("@#{key}", value) }
19
+ define_singleton_method("#{key}=") { |value| instance_variable_set("@#{key}", value) }
20
20
  end
21
21
  end
22
22
  yield(config) if block_given?
@@ -0,0 +1,3 @@
1
+ motion_require 'stubs/date'
2
+ motion_require 'stubs/date_time'
3
+ motion_require 'stubs/decimal'
@@ -0,0 +1,2 @@
1
+ class Date
2
+ end
@@ -0,0 +1,2 @@
1
+ class DateTime
2
+ end
@@ -0,0 +1,2 @@
1
+ class BigDecimal
2
+ end
@@ -71,13 +71,12 @@ module Coercible
71
71
  #
72
72
  # @api private
73
73
  def define_option_method(option)
74
- metaclass = class << self; self; end
75
- metaclass.instance_eval do
74
+ self.class.instance_eval do
76
75
  define_method("#{option}") do |value=Undefined|
77
76
  return instance_variable_get("@#{option}") if value.equal?(Undefined)
78
77
  instance_variable_set("@#{option}", value)
79
- metaclass
80
- end
78
+ self
79
+ end
81
80
  end
82
81
  end
83
82
 
@@ -40,9 +40,9 @@ module Coercible
40
40
  # @return [Class]
41
41
  #
42
42
  # @api private
43
- def primitive
44
- raise NotImplementedError, "#{self}.primitive must be implemented"
45
- end
43
+ #def primitive
44
+ # raise NotImplementedError, "#{self}.primitive must be implemented"
45
+ #end
46
46
 
47
47
  private
48
48
 
@@ -1,3 +1,3 @@
1
1
  module Coercible
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.0.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion_coercible
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Closner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-28 00:00:00.000000000 Z
11
+ date: 2013-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: motion-require
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: motion_descendants_tracker
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ! '>='
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ! '>='
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -63,6 +49,9 @@ files:
63
49
  - lib/motion_coercible.rb
64
50
  - lib/project/coercer/array.rb
65
51
  - lib/project/coercer/configurable.rb
52
+ - lib/project/coercer/date.rb
53
+ - lib/project/coercer/date_time.rb
54
+ - lib/project/coercer/decimal.rb
66
55
  - lib/project/coercer/false_class.rb
67
56
  - lib/project/coercer/float.rb
68
57
  - lib/project/coercer/hash.rb
@@ -77,6 +66,10 @@ files:
77
66
  - lib/project/coercer.rb
78
67
  - lib/project/coercible.rb
79
68
  - lib/project/configuration.rb
69
+ - lib/project/stubs/date.rb
70
+ - lib/project/stubs/date_time.rb
71
+ - lib/project/stubs/decimal.rb
72
+ - lib/project/stubs.rb
80
73
  - lib/project/support/options.rb
81
74
  - lib/project/support/type_lookup.rb
82
75
  - lib/project/version.rb