attr_typed 1.0.0

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: 63551aa968c5f70cc12e5f78968504af5fdd684d
4
+ data.tar.gz: a85087575bed96f0853bd590a3ef101e49dd3a9d
5
+ SHA512:
6
+ metadata.gz: 6561f15f3e79fdd38f8dcadbdaf26e09fd81881f278e07578ccaeae41c2d0216fa73045879ae5e24a9ee57b23aa6d6de107026ad7f561d2d884213af3c6d1c7f
7
+ data.tar.gz: 85c3925fed636794081343ce086294179a72e10702129f1c3e82c09fe4a651c1888d2658d14bd68d65d622a125d557c5a794cc4865a65245bfbcaa4da2e8a77a
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attr_typed.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ferocia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # AttrTyped
2
+
3
+ A way to add strong typing support to your ruby attributes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'attr_typed'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install attr_typed
20
+
21
+ ## Usage
22
+
23
+ Include the `AttrTyped` module in your class then declare your types like this:
24
+
25
+ ```
26
+ attr_typed name: :string,
27
+ amount: :money,
28
+ created_at: :time
29
+
30
+ ```
31
+
32
+ The currently supported types are:
33
+
34
+ ```
35
+ :string, :money, :time, :big_decimal, :date, :integer, :boolean, :date_time
36
+ ```
37
+
38
+ The parsing behaves in a very predictable way, the only exception is when
39
+ parsing a string of `"y"` (i.e. yes) to a boolean it will be true.
40
+
41
+ If you want to log parsing failures, use `AttrTyped.logger = MyLogger`.
42
+
43
+ If you want to use `:time` parsing you need to use `ActiveSupport` and have a
44
+ default time zone setup.
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bundle` to install dependencies. Then, run `rake spec` to run the tests.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ferocia/attr_typed.
55
+
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
60
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attr_typed/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attr_typed"
8
+ spec.version = AttrTyped::VERSION
9
+ spec.authors = ["Anthony Langhorne"]
10
+ spec.email = ["anthony.langhorne@ferocia.com.au"]
11
+
12
+ spec.summary = %q{Enforce attribute types at assignment time}
13
+ spec.homepage = "https://github.com/Ferocia/attr_typed"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency 'monetize', '>= 1.0.0'
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ module AttrTyped
2
+ VERSION = "1.0.0"
3
+ end
data/lib/attr_typed.rb ADDED
@@ -0,0 +1,113 @@
1
+ require "monetize"
2
+ require "money"
3
+ require "bigdecimal"
4
+ require "date"
5
+
6
+ # Ability to mix in some strong typing support for attributes
7
+ #
8
+ # Usage:
9
+ # attr_typed my_field: money,
10
+ # another_field: time
11
+ #
12
+ # Then on assigning my_field = "1.23", my_field will be co-erced to Money.new(123) ($1.23)
13
+ #
14
+ # Gotchas:
15
+ # - Don't use instance variable assignment (Use self.my_field= as opposed to @my_field=)
16
+ # - Due to the way string.to_f works in ruby, self.my_field = "cats" will equal $0.00
17
+ #
18
+ module AttrTyped
19
+ ALLOWED_TYPES ||= [
20
+ :string, :money, :time, :big_decimal, :date, :integer, :boolean, :date_time
21
+ ]
22
+
23
+ def self.included(klass)
24
+ klass.extend(ClassMethods)
25
+ end
26
+
27
+ class << self
28
+ attr_accessor :logger
29
+ end
30
+
31
+ def parse_typed_value(value, type)
32
+ public_send("parse_#{type}", value) unless value.nil?
33
+ rescue ArgumentError => e
34
+ if AttrTyped.logger
35
+ AttrTyped.logger.error("Error parsing '#{value}' into a #{type}")
36
+ end
37
+ raise e
38
+ end
39
+
40
+ def parse_string(value)
41
+ return value if value.is_a?(String)
42
+
43
+ value.to_s
44
+ end
45
+
46
+ def parse_integer(value)
47
+ value.to_i
48
+ end
49
+
50
+ def parse_date(value)
51
+ return value if value.is_a?(Date)
52
+
53
+ if Time.respond_to?(:zone)
54
+ Time.zone.parse(value).to_date
55
+ else
56
+ Date.parse(value)
57
+ end
58
+ end
59
+
60
+ def parse_date_time(value)
61
+ return value if value.is_a?(DateTime)
62
+
63
+ DateTime.parse(value)
64
+ end
65
+
66
+ def parse_big_decimal(value)
67
+ return value if value.is_a?(BigDecimal)
68
+ return value.to_d if value.is_a?(Float)
69
+
70
+ BigDecimal.new(value)
71
+ end
72
+
73
+ def parse_money(value)
74
+ return value if value.is_a?(Money)
75
+
76
+ Monetize.from_bigdecimal(BigDecimal.new(value.to_s))
77
+ end
78
+
79
+ def parse_time(value)
80
+ return value if value.is_a?(Time)
81
+
82
+ raise "ActiveSupport with a time zone set is required" unless Time.respond_to?(:zone)
83
+
84
+ Time.zone.parse(value)
85
+ end
86
+
87
+ def parse_boolean(value)
88
+ return if value.nil? && !value.is_a?(FalseClass)
89
+ return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)
90
+
91
+ ["true", "y"].include?(value.to_s.downcase)
92
+ end
93
+
94
+ # Class method attr_typed
95
+ module ClassMethods
96
+ def attr_typed(attrs)
97
+ attrs.each do |(attribute, type)|
98
+ raise ArgumentError, "Unsupported type #{type}" unless ALLOWED_TYPES.include?(type)
99
+
100
+ define_method("#{attribute}_with_typing=") do |value|
101
+ instance_variable_set("@#{attribute}", public_send("parse_typed_value", value, type))
102
+ end
103
+
104
+ class_eval do
105
+ attr_accessor attribute
106
+
107
+ alias_method "#{attribute}_without_typing=", "#{attribute}="
108
+ alias_method "#{attribute}=", "#{attribute}_with_typing="
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_typed
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Langhorne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: monetize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.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.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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:
70
+ email:
71
+ - anthony.langhorne@ferocia.com.au
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - attr_typed.gemspec
82
+ - lib/attr_typed.rb
83
+ - lib/attr_typed/version.rb
84
+ homepage: https://github.com/Ferocia/attr_typed
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.5
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Enforce attribute types at assignment time
108
+ test_files: []