active_model_coercions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f26147cc624dd76b9f62f02f7315e54525a6f98
4
+ data.tar.gz: 443fb001d80f8d2ba77a48167e9f0e6aa7289d91
5
+ SHA512:
6
+ metadata.gz: e0646ea553b957df2985a6d21d6cc0f9987fa743667f49fe68791533c85b0adf8c6682d827274d13bc94ffc5f6060ba7d5ae8c17c79ebcce7c9c96e2589256f5
7
+ data.tar.gz: 2e3ebc6ed4f032456bb88532b22231400d44d7b6dc1ff04f6a5843a4e0c3eb0319bc210a25bf05948ce1a0083a7feb565f3396ecc644a580cc195294ec1d8172
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_model_coercions.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Marcin Olichwirowicz
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.
@@ -0,0 +1,59 @@
1
+ # ActiveModelCoercions
2
+
3
+ Currently, it's just an experimental coercion proof of concept for ActiveModel objects. However if you see some areas of improvement or want to discuss some stuff - feel free to open an issue/pull request.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_model_coercions'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_model_coercions
20
+
21
+ ## Usage
22
+
23
+
24
+ ```ruby
25
+ class TestModel
26
+ include ActiveModel::Model
27
+ include ActiveModel::Coercions
28
+
29
+
30
+ attr_accessor :number, :date, :state
31
+
32
+ attribute :number, :integer
33
+ attribute :state, :symbol
34
+
35
+ end
36
+ ```
37
+
38
+ ```ruby
39
+ params = {number: '1', state: 'active'}
40
+ model = TestModel.new(params)
41
+
42
+ model.number # => 1
43
+ model.state # => :active
44
+ model.attributes # => {number: 1, state: :active}
45
+
46
+ ```
47
+
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (git checkout -b feature-branch)
53
+ 3. Commit your changed (git commit -a -m 'Add some changes')
54
+ 4. Push feature branch to github (git push origin feature-branch)
55
+ 5. Create pull request
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_model/coercions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_model_coercions"
8
+ spec.version = ActiveModel::Coercions::VERSION
9
+ spec.authors = ["Marcin Olichwirowicz"]
10
+ spec.email = ["olichwirowicz@gmail.com"]
11
+
12
+ spec.summary = %q{Provides coercions for ActiveModel objects}
13
+ spec.description = %q{Provides coercions for ActiveModel objects}
14
+ spec.homepage = "https://github.com/rodzyn/active_model_coercions"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activemodel", "5.0.0.alpha"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_model_coercions"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,20 @@
1
+ module ActiveModel
2
+ module AttributeAssignment
3
+ private
4
+ def _assign_attribute(k, v)
5
+ if respond_to?("#{k}=")
6
+ public_send("#{k}=", _assign_attribute_value(k, v))
7
+ else
8
+ raise UnknownAttributeError.new(self, k)
9
+ end
10
+ end
11
+
12
+ def _assign_attribute_value(key, value)
13
+ if self.class.declared_coercions.include?(key.to_sym)
14
+ self.attributes[key.to_sym] = self.class.declared_coercions[key.to_sym].cast(value)
15
+ else
16
+ self.attributes[key.to_sym] = value
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ require 'active_support/core_ext/string/conversions'
2
+
3
+ module ActiveModel
4
+ class Coercer
5
+ def initialize(type)
6
+ @coercion_type = self.class.defined_coercions.fetch(type)
7
+ end
8
+
9
+ def cast(value)
10
+ @coercion_type.new(value).to_value
11
+ end
12
+
13
+ class << self
14
+ def defined_coercions
15
+ {
16
+ integer: ActiveModel::Coercions::Types::Integer,
17
+ float: ActiveModel::Coercions::Types::Float,
18
+ symbol: ActiveModel::Coercions::Types::Symbol,
19
+ time: ActiveModel::Coercions::Types::Time,
20
+ date: ActiveModel::Coercions::Types::Date,
21
+ datetime: ActiveModel::Coercions::Types::DateTime,
22
+ boolean: ActiveModel::Coercions::Types::Boolean
23
+ }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ attr_accessor :attributes
4
+
5
+ class << self
6
+ def included(base)
7
+ base.extend(ClassMethods)
8
+ base.class_eval { @declared_coercions = {} }
9
+ end
10
+ end
11
+
12
+ def attributes
13
+ @attributes ||= {}
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ attr_accessor :declared_coercions
19
+
20
+ def attribute(attr, type)
21
+ @declared_coercions[attr] = Coercer.new(type)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ module Types
4
+ class Boolean
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def to_value
10
+ @value.to_time
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ module Types
4
+ class Date
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def to_value
10
+ @value.to_date
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ module Types
4
+ class DateTime
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def to_value
10
+ @value.to_datetime
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ module Types
4
+ class Float
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def to_value
10
+ @value.to_f
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ module Types
4
+ class Integer
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def to_value
10
+ @value.to_i
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ module Types
4
+ class Symbol
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def to_value
10
+ @value.to_sym
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ module Types
4
+ class Time
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def to_value
10
+ @value.to_time
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ module Coercions
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_model';
2
+
3
+ require 'active_model/coercions/version'
4
+ require 'active_model/coercions'
5
+ require 'active_model/coercer'
6
+
7
+ require 'active_model/assign_attributes'
8
+
9
+ require 'active_model/coercions/types/float'
10
+ require 'active_model/coercions/types/integer'
11
+ require 'active_model/coercions/types/date'
12
+ require 'active_model/coercions/types/datetime'
13
+ require 'active_model/coercions/types/symbol'
14
+ require 'active_model/coercions/types/time'
15
+ require 'active_model/coercions/types/boolean'
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model_coercions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Olichwirowicz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0.alpha
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0.alpha
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: Provides coercions for ActiveModel objects
70
+ email:
71
+ - olichwirowicz@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - active_model_coercions.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/active_model/assign_attributes.rb
88
+ - lib/active_model/coercer.rb
89
+ - lib/active_model/coercions.rb
90
+ - lib/active_model/coercions/types/boolean.rb
91
+ - lib/active_model/coercions/types/date.rb
92
+ - lib/active_model/coercions/types/datetime.rb
93
+ - lib/active_model/coercions/types/float.rb
94
+ - lib/active_model/coercions/types/integer.rb
95
+ - lib/active_model/coercions/types/symbol.rb
96
+ - lib/active_model/coercions/types/time.rb
97
+ - lib/active_model/coercions/version.rb
98
+ - lib/active_model_coercions.rb
99
+ homepage: https://github.com/rodzyn/active_model_coercions
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.4.8
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Provides coercions for ActiveModel objects
123
+ test_files: []
124
+ has_rdoc: