typecast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae264bc787481a2a893d02fd669bbf8cab35ac56
4
+ data.tar.gz: 3812c7ad940d5365ad520d7ab5461e6e3426d0b7
5
+ SHA512:
6
+ metadata.gz: 2892564732a97685768a0a3440c4d8456dd0a123fbbaff91d90361423dca215148cd453ee138092f7f0c771901c5d1d550a2c4f12bc8598644bae60a6bac5dcd
7
+ data.tar.gz: 97a7db182f2232abe219dfdbe77245745c7a2743a6a0f2dd4999cc65d37e7104eb731e08845cadf92eb7b20a7f79f90f9143fddb5645bd41cbb9b0c677eeaf5a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in typecast.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex Avoyants
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Typecast
2
+
3
+ Type casting of attributes defined in superclass or realized through method_missing. Works only for string attributes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'typecast'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install typecast
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class User
23
+ def orders_count
24
+ "4"
25
+ end
26
+
27
+ def aov
28
+ "207.56"
29
+ end
30
+
31
+ def created_at
32
+ "2014-05-26T19:31:04+03:00"
33
+ end
34
+
35
+ def updated_at
36
+ "2014-05-26 19:49:59 +0300"
37
+ end
38
+
39
+ def expires_at
40
+ "26 September 2014 at 19:50:38 EEST"
41
+ end
42
+
43
+ def method_missing(method_name, *args, &block)
44
+ return super unless "dynamic_created_at" == method_name.to_s
45
+ "2014-05-27T13:19:25+03:00"
46
+ end
47
+ end
48
+
49
+ class Customer < User
50
+ include TypeCast
51
+
52
+ type_cast :expires_at, DateTime # performs type casting of ancestor's instance method with object which respond_to? parse method
53
+ type_cast :created_at, :updated_at, Time # several attributes provided at once
54
+ type_cast :dynamic_created_at, Time # attribute that works through method_missing
55
+ type_cast :orders_count, :to_i
56
+ type_cast :aov, :to_f
57
+ end
58
+
59
+ customer = Customer.new
60
+ customer.expires_at # => DateTime.parse("26 September 2014 at 19:50:38 EEST")
61
+ customer.created_at # => Time.parse("2014-05-26T19:31:04+03:00")
62
+ customer.updated_at # => Time.parse("2014-05-26 19:49:59 +0300")
63
+ customer.dynamic_created_at # => Time.parse("2014-05-27T13:19:25+03:00")
64
+ customer.orders_count # => 4
65
+ customer.aov # => 207.56
66
+
67
+ customer.created_at_before_type_cast # => "2014-05-26T19:31:04+03:00"
68
+ customer.orders_count_before_type_cast # => "4"
69
+ ```
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it ( http://github.com/shhavel/typecast/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.rspec_opts = ["-c", "-f documentation", "-r ./spec/spec_helper.rb"]
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ module TypeCast
2
+ VERSION = "0.0.1"
3
+ end
data/lib/type_cast.rb ADDED
@@ -0,0 +1,72 @@
1
+ require "type_cast/version"
2
+ require "active_support"
3
+
4
+ module TypeCast
5
+ extend ::ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def type_cast(*args)
9
+ caster = args.pop
10
+ if args.empty?
11
+ raise ArgumentError, 'Instance methods for type casting supposed to be provided (e.g. type_cast :created_at, :updated_at, Time).'
12
+ end
13
+ if caster.respond_to?(:parse)
14
+ args.each do |attribute|
15
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
+ def #{attribute}
17
+ @#{attribute}_after_type_cast ||= begin
18
+ uncasted = if defined?(super)
19
+ super
20
+ else
21
+ method_missing(:#{attribute})
22
+ end
23
+ self.#{attribute}_before_type_cast = uncasted
24
+ if uncasted.kind_of?(String)
25
+ #{caster}.parse(uncasted)
26
+ else
27
+ uncasted
28
+ end
29
+ rescue
30
+ uncasted
31
+ end
32
+ end
33
+ attr_writer :#{attribute}_before_type_cast
34
+ def #{attribute}_before_type_cast
35
+ @#{attribute}_before_type_cast || (#{attribute}; #{attribute}_before_type_cast)
36
+ end
37
+ RUBY
38
+ end
39
+ elsif caster.respond_to?(:to_sym)
40
+ conversion_method = caster.to_sym
41
+ args.each do |attribute|
42
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
43
+ def #{attribute}
44
+ @#{attribute}_after_type_cast ||= begin
45
+ uncasted = if defined?(super)
46
+ super
47
+ else
48
+ method_missing(:#{attribute})
49
+ end
50
+ self.#{attribute}_before_type_cast = uncasted
51
+ if uncasted.kind_of?(String) && uncasted.respond_to?(:#{conversion_method})
52
+ uncasted.__send__(:#{conversion_method})
53
+ else
54
+ uncasted
55
+ end
56
+ rescue
57
+ uncasted
58
+ end
59
+ end
60
+ attr_writer :#{attribute}_before_type_cast
61
+ def #{attribute}_before_type_cast
62
+ @#{attribute}_before_type_cast || (#{attribute}; #{attribute}_before_type_cast)
63
+ end
64
+ RUBY
65
+ end
66
+ else
67
+ raise ArgumentError, 'Type casting needs an object with parse method or method name which should be called on casted attributes. Supply it as last argument (e.g. type_cast :created_at, Time).'
68
+ end
69
+ end
70
+ alias_method :typecast, :type_cast
71
+ end
72
+ end
data/lib/typecast.rb ADDED
@@ -0,0 +1 @@
1
+ require 'type_cast'
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+ require "type_cast"
3
+ require "date"
4
+
5
+ describe Customer do
6
+ it "performs type casting of ancestor's instance method with object which respond_to? parse method" do
7
+ expect(subject.expires_at).to eq DateTime.parse("26 September 2014 at 19:50:38 EEST")
8
+ end
9
+
10
+ it "performs type casting of several attributes provided at once" do
11
+ expect(subject.created_at).to eq Time.parse("2014-05-26T19:31:04+03:00")
12
+ expect(subject.updated_at).to eq Time.parse("2014-05-26 19:49:59 +0300")
13
+ end
14
+
15
+ it "performs type casting of attribute that works through method_missing" do
16
+ expect(subject.dynamic_created_at).to eq Time.parse("2014-05-27T13:19:25+03:00")
17
+ end
18
+
19
+ it "performs type casting of ancestor's instance method providing with method name which should be called on that" do
20
+ expect(subject.orders_count).to eq 4
21
+ expect(subject.aov).to eq 207.56
22
+ end
23
+
24
+ it "raises exception if last argument doesn't respont to parse method and doesn't respont to :to_sym" do
25
+ expect { described_class.class_eval { type_cast :name, Array } }.to raise_error(ArgumentError)
26
+ end
27
+
28
+ it "raises exception if no arguments for type casting provided" do
29
+ expect { described_class.class_eval { type_cast Time } }.to raise_error(ArgumentError)
30
+ end
31
+
32
+ it "declares a method for attribute with the *_before_type_cast suffix" do
33
+ expect(subject.created_at_before_type_cast).to eq "2014-05-26T19:31:04+03:00"
34
+ expect(subject.dynamic_created_at_before_type_cast).to eq "2014-05-27T13:19:25+03:00"
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ require "type_cast"
2
+
3
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'user'
2
+
3
+ class Customer < User
4
+ include TypeCast
5
+
6
+ type_cast :expires_at, DateTime
7
+ type_cast :created_at, :updated_at, Time
8
+ type_cast :orders_count, :to_i
9
+ type_cast :aov, :to_f
10
+ type_cast :dynamic_created_at, Time
11
+ end
@@ -0,0 +1,26 @@
1
+ class User
2
+ def orders_count
3
+ "4"
4
+ end
5
+
6
+ def aov
7
+ "207.56"
8
+ end
9
+
10
+ def created_at
11
+ "2014-05-26T19:31:04+03:00"
12
+ end
13
+
14
+ def updated_at
15
+ "2014-05-26 19:49:59 +0300"
16
+ end
17
+
18
+ def expires_at
19
+ "26 September 2014 at 19:50:38 EEST"
20
+ end
21
+
22
+ def method_missing(method_name, *args, &block)
23
+ return super unless "dynamic_created_at" == method_name.to_s
24
+ "2014-05-27T13:19:25+03:00"
25
+ end
26
+ end
data/typecast.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'type_cast/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "typecast"
8
+ spec.version = TypeCast::VERSION
9
+ spec.authors = ["Alex Avoyants"]
10
+ spec.email = ["shhavel@gmail.com"]
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.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typecast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Avoyants
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-27 00:00:00.000000000 Z
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
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Type casting of attributes defined in superclass or realized through
70
+ method_missing. Works only for string attributes.
71
+ email:
72
+ - shhavel@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/type_cast.rb
84
+ - lib/type_cast/version.rb
85
+ - lib/typecast.rb
86
+ - spec/customer_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/support/customer.rb
89
+ - spec/support/user.rb
90
+ - typecast.gemspec
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Type casting of attributes defined in superclass or realized through method_missing.
115
+ test_files:
116
+ - spec/customer_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/support/customer.rb
119
+ - spec/support/user.rb