active_model_attributes 0.1.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: 9d343aa6037f2b68e028b565f125166ed52da7a0
4
+ data.tar.gz: 6ba50645e315b6ea90c9e79d6a80dec54f1f0732
5
+ SHA512:
6
+ metadata.gz: 07c010aa7b7c2049460e3cdce9362d3e977effd266976fd4bc0cd08069ebd0b8639feb1b3d9079dcd4a6078bff4eb4389895634f10c4d9e87aa16511414ed079
7
+ data.tar.gz: 660e2fcc7935ca4bc3876777680b32609d20e5171c9a59a8f2067b9f2ed2bdc41007eaca73e8ca71be0015990403375770b7d1b2ca0cf96772dd0d035d9d7588
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -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
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_model_attributes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Karol Galanciak
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,126 @@
1
+ # ActiveModelAttributes [![Build Status](https://travis-ci.org/Azdaroth/active_model_attributes.svg?branch=master)](https://travis-ci.org/Azdaroth/active_model_attributes) [![Coverage Status](https://coveralls.io/repos/github/Azdaroth/active_model_attributes/badge.svg)](https://coveralls.io/github/Azdaroth/active_model_attributes)
2
+
3
+ Rails 5.0 comes with a great addition of ActiveRecord Attributes API. However, that's only for ActiveRecord, you can't really use it in you ActiveModel models. Fortunately, with this gem it's possible.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_model_attributes'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_model_attributes
20
+
21
+ ## Usage
22
+
23
+ Define you ActiveModel model class, include `ActiveModel::Model` and `ActiveModelAttributes` modules and define attributes and their types using `attribute` class method:
24
+
25
+ ``` rb
26
+ class MyAwesomeModel
27
+ include ActiveModel::Model
28
+ include ActiveModelAttributes
29
+
30
+ attribute :integer_field, :integer
31
+ attribute :string_field, :string
32
+ attribute :decimal_field, :decimal
33
+ attribute :boolean_field, :boolean
34
+ end
35
+ ```
36
+
37
+ You can also provide a default value for each attribute (either a raw value or a lambda):
38
+
39
+ ``` rb
40
+ class MyAwesomeModel
41
+ include ActiveModel::Model
42
+ include ActiveModelAttributes
43
+
44
+ attribute :string_with_default, :string, default: "default string"
45
+ attribute :date_field, :date, default: -> { Date.new(2016, 1, 1) }
46
+ end
47
+ ```
48
+
49
+ You can get the list of defined attributes, their types and provided options by accessing `attributes_registry` class attribute, for instance:
50
+
51
+ ``` rb
52
+ class MyAwesomeModel
53
+ include ActiveModel::Model
54
+ include ActiveModelAttributes
55
+
56
+ attribute :string_with_default, :string, default: "default string"
57
+ end
58
+ ```
59
+
60
+ ```
61
+ MyAwesomeModel.attributes_registry
62
+ => { string_with_default: [:string, { default: "default string" }] }
63
+ ```
64
+
65
+ Here's a list of supported types:
66
+
67
+ * big_integer
68
+ * binary
69
+ * boolean
70
+ * date
71
+ * datetime
72
+ * decimal
73
+ * float
74
+ * immutable_string
75
+ * integer
76
+ * string
77
+ * text
78
+ * time
79
+
80
+ You can also add your custom types. Just create a class inheriting from `ActiveModel::Type::Value` or already existing type, e.g. `ActiveModel::Type::Integer`, define `deserialize` method and register the new type:
81
+
82
+ ``` rb
83
+ class SomeCustomMoneyType < ActiveModel::Type::Integer
84
+ def deserialize(value)
85
+ return super if value.kind_of?(Numeric)
86
+ return super if !value.to_s.include?('$')
87
+
88
+ price_in_dollars = BigDecimal.new(value.gsub(/\$/, ''))
89
+ super(price_in_dollars * 100)
90
+ end
91
+ end
92
+
93
+ ActiveModel::Type.register(:money, SomeCustomMoneyType)
94
+ ```
95
+
96
+ Now you can use this type inside you ActiveModel models:
97
+
98
+ ```
99
+ class ModelForAttributesTestWithCustomType
100
+ include ActiveModel::Model
101
+ include ActiveModelAttributes
102
+
103
+ attribute :price, :money
104
+ end
105
+
106
+ data = ModelForAttributesTestWithCustomType.new
107
+ data.price = "$100.12"
108
+ data.price
109
+ => 10012
110
+ ```
111
+
112
+ ## Development
113
+
114
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
115
+
116
+ 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).
117
+
118
+ ## Contributing
119
+
120
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_model_attributes.
121
+
122
+
123
+ ## License
124
+
125
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
126
+
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,33 @@
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_attributes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_model_attributes"
8
+ spec.version = ActiveModelAttributes::VERSION
9
+ spec.authors = ["Karol Galanciak"]
10
+ spec.email = ["karol.galanciak@gmail.com"]
11
+
12
+ spec.summary = %q{ActiveModel extension with support for ActiveRecord-like Attributes API}
13
+ spec.description = %q{ActiveModel extension with support for ActiveRecord-like Attributes API}
14
+ spec.homepage = "https://github.com/Azdaroth/active_model_attributes"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "pry-byebug"
29
+ spec.add_development_dependency "coveralls"
30
+
31
+ spec.add_dependency "activemodel", ">= 5.0"
32
+ spec.add_dependency "activesupport", ">= 5.0"
33
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_model_attributes"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ require "active_model_attributes/version"
2
+ require "active_support/concern"
3
+ require "active_model/type"
4
+
5
+ module ActiveModelAttributes
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :attributes_registry, instance_accessor: false
10
+ self.attributes_registry = {}
11
+ end
12
+
13
+ module ClassMethods
14
+ NO_DEFAULT_PROVIDED = Object.new
15
+ private_constant :NO_DEFAULT_PROVIDED
16
+
17
+ def attribute(name, cast_type, **options)
18
+ self.attributes_registry = attributes_registry.merge(name => [cast_type, options])
19
+
20
+ define_attribute_reader(name, options)
21
+ define_attribute_writer(name, cast_type, options)
22
+ end
23
+
24
+ def define_attribute_reader(name, options)
25
+ provided_default = options.fetch(:default) { NO_DEFAULT_PROVIDED }
26
+ define_method name do
27
+ return instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}")
28
+ return if provided_default == NO_DEFAULT_PROVIDED
29
+ provided_default.respond_to?(:call) && provided_default.call || provided_default
30
+ end
31
+ end
32
+
33
+ def define_attribute_writer(name, cast_type, options)
34
+ define_method "#{name}=" do |val|
35
+ deserialized_value = ActiveModel::Type.lookup(cast_type).deserialize(val)
36
+ instance_variable_set("@#{name}", deserialized_value)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveModelAttributes
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karol Galanciak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activemodel
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '5.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '5.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '5.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '5.0'
125
+ description: ActiveModel extension with support for ActiveRecord-like Attributes API
126
+ email:
127
+ - karol.galanciak@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".coveralls.yml"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - active_model_attributes.gemspec
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/active_model_attributes.rb
144
+ - lib/active_model_attributes/version.rb
145
+ homepage: https://github.com/Azdaroth/active_model_attributes
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.5.1
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: ActiveModel extension with support for ActiveRecord-like Attributes API
169
+ test_files: []