typecast 0.0.2 → 0.0.3

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ab8f1650d1e14de428e215bf8c11c26738eae07
4
- data.tar.gz: fb46b3866ea380cb935c9109c4348e7e12150a1f
3
+ metadata.gz: 7fa8da88f74a1ff3918e46c1cc786b5607c0c16c
4
+ data.tar.gz: ed189867a427596b58f8f3cf5d03320b9abc51a6
5
5
  SHA512:
6
- metadata.gz: 9bf81694ebce5752c7d31a114238a94b3f5d0636f6ea8dd1f75a715b9f6eca7c225f94e450bcf09c10d5b3b61ed0528aaee97d21b8627744392b8056e894af6b
7
- data.tar.gz: 8d740bf2c55fe09af27432a73cce9ad4f8910c1f16741bb59765fff5ebf2f0a947df832754f362148b45ea8dfdbb5ac2171020a6a9fa0251245f34b283614235
6
+ metadata.gz: 6d3fb81bbf99c3b75a79ee527390b8f95f80d8d9e790bbdf10de133f734ec15aee04d972d0e11195ed719e1d839e920ca9c4e1520da8627a3850825485fc5aad
7
+ data.tar.gz: 1f372192553d27c536d43b3d57286bc7ffee3ea8a2bb10da41bfc8a9b34e5120e81e6345ba864371f12d465a8035e691421e7b62fba74480e2211c3ef34c9334
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # TypeCast
2
2
 
3
- Type casting of attributes defined in superclass or realized through method_missing. Works only for string attributes.
3
+ Type casting of attributes defined in superclass or realized through method_missing.<br />
4
+ Converts string attributes with parser object (object that respond_to? :parse)<br />
5
+ or attribute of any type with type-casting method (e.g. :to_i, :to_f).
4
6
 
5
7
  ## Installation
6
8
 
@@ -40,6 +42,10 @@ class User
40
42
  "26 September 2014 at 19:50:38 EEST"
41
43
  end
42
44
 
45
+ def suggestions_count
46
+ nil
47
+ end
48
+
43
49
  def method_missing(method_name, *args, &block)
44
50
  return super unless "dynamic_created_at" == method_name.to_s
45
51
  "2014-05-27T13:19:25+03:00"
@@ -52,7 +58,7 @@ class Customer < User
52
58
  type_cast :expires_at, DateTime # performs type casting of ancestor's instance method with object which respond_to? parse method
53
59
  type_cast :created_at, :updated_at, Time # several attributes provided at once
54
60
  type_cast :dynamic_created_at, Time # attribute that works through method_missing
55
- type_cast :orders_count, :to_i
61
+ type_cast :orders_count, :suggestions_count, :to_i
56
62
  type_cast :aov, :to_f
57
63
  end
58
64
 
@@ -62,12 +68,91 @@ customer.created_at # => Time.parse("2014-05-26T19:31:04+03:00")
62
68
  customer.updated_at # => Time.parse("2014-05-26 19:49:59 +0300")
63
69
  customer.dynamic_created_at # => Time.parse("2014-05-27T13:19:25+03:00")
64
70
  customer.orders_count # => 4
71
+ customer.suggestions_count # => 0
65
72
  customer.aov # => 207.56
66
73
 
67
74
  customer.created_at_before_type_cast # => "2014-05-26T19:31:04+03:00"
68
75
  customer.orders_count_before_type_cast # => "4"
69
76
  ```
70
77
 
78
+ ## History :)
79
+
80
+ Gem was originally created to use with ActiveRecourse, because next stuff don't work
81
+
82
+ ```ruby
83
+ class User < ActiveResource::Base
84
+ attribute 'created_at', :time # doesn't work
85
+ attribute 'created_at', :datetime # doesn't work
86
+
87
+ # doesn't work
88
+ schema do
89
+ datetime :created_at
90
+ end
91
+
92
+ # doesn't work
93
+ schema do
94
+ time :created_at
95
+ end
96
+ end
97
+ ```
98
+
99
+ Next solution works but has some issue
100
+
101
+ ```ruby
102
+ # It check all atributes and parses to DateTime all that matches datetime regexp.
103
+ ActiveSupport::JSON::Encoding.use_standard_json_time_format = true
104
+ ActiveSupport.parse_json_times = true
105
+ ```
106
+
107
+ But ruby's [DateTime.strftime](http://apidock.com/ruby/DateTime/strftime) method has minor bug: `DateTime.strftime('%Z')` [output is incorrect format](https://www.ruby-forum.com/topic/4349831)
108
+
109
+ ```ruby
110
+ Time.now.strftime('%Z') # => "EEST"
111
+ DateTime.now.strftime('%Z') # => "+03:00"
112
+ ```
113
+
114
+ You can monkey patch `ActiveSupport` to use `Time.parse` insted of `DateTime.parse` to avoid this issue, but it still be slower
115
+
116
+ ```ruby
117
+ module ActiveSupport
118
+ module JSON
119
+ class << self
120
+ private
121
+ def convert_dates_from(data)
122
+ case data
123
+ when nil
124
+ nil
125
+ when DATE_REGEX
126
+ begin
127
+ Time.parse(data)
128
+ rescue ArgumentError
129
+ data
130
+ end
131
+ when Array
132
+ data.map! { |d| convert_dates_from(d) }
133
+ when Hash
134
+ data.each do |key, value|
135
+ data[key] = convert_dates_from(value)
136
+ end
137
+ else
138
+ data
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ ```
145
+
146
+ Among cons of the proposed solution is that you'll need add type casting to all models.<br/>
147
+ Or perhaps `type_cast` common attributes in `ActiveResource::Base`
148
+
149
+ ```ruby
150
+ class ActiveResource::Base
151
+ include TypeCast
152
+ type_cast :created_at, :updated_at, Time
153
+ end
154
+ ```
155
+
71
156
  ## Contributing
72
157
 
73
158
  1. Fork it ( http://github.com/shhavel/typecast/fork )
@@ -1,8 +1,9 @@
1
1
  require "type_cast/version"
2
- require "active_support"
3
2
 
4
3
  module TypeCast
5
- extend ::ActiveSupport::Concern
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
6
7
 
7
8
  module ClassMethods
8
9
  def type_cast(*args)
@@ -70,3 +71,12 @@ module TypeCast
70
71
  alias_method :typecast, :type_cast
71
72
  end
72
73
  end
74
+
75
+ begin
76
+ require 'active_resource'
77
+ class ActiveResource::Base
78
+ include TypeCast
79
+ type_cast :created_at, :updated_at, Time
80
+ end
81
+ rescue LoadError
82
+ end
@@ -1,3 +1,3 @@
1
1
  module TypeCast
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Alex Avoyants"]
10
10
  spec.email = ["shhavel@gmail.com"]
11
11
  spec.summary = %q{Type casting of attributes defined in superclass or realized through method_missing.}
12
- spec.description = %q{Type casting of attributes defined in superclass or realized through method_missing. Works only for string attributes.}
12
+ spec.description = %q{ype casting of attributes defined in superclass or realized through method_missing. Converts string attributes with parser object (object that respond_to? :parse) or attribute of any type with type-casting method (e.g. :to_i, :to_f).}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
@@ -18,7 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "activesupport"
22
21
  spec.add_development_dependency "bundler", "~> 1.5"
23
22
  spec.add_development_dependency "rake"
24
23
  spec.add_development_dependency "rspec"
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typecast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Avoyants
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-02 00:00:00.000000000 Z
11
+ date: 2015-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activesupport
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: bundler
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -66,8 +52,9 @@ dependencies:
66
52
  - - '>='
67
53
  - !ruby/object:Gem::Version
68
54
  version: '0'
69
- description: Type casting of attributes defined in superclass or realized through
70
- method_missing. Works only for string attributes.
55
+ description: ype casting of attributes defined in superclass or realized through method_missing.
56
+ Converts string attributes with parser object (object that respond_to? :parse) or
57
+ attribute of any type with type-casting method (e.g. :to_i, :to_f).
71
58
  email:
72
59
  - shhavel@gmail.com
73
60
  executables: []