active_attr_extended 0.9.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: 3d74bf1de85a947c2a3db19d89c6140f9d6ae713
4
+ data.tar.gz: 92b44b2e16e8b5c76e9b9d6ebb2df92aabaedea6
5
+ SHA512:
6
+ metadata.gz: 520d3034332dc9e3de7e967118930b9978e343e7a06aa75f98e1c4e583b429965ac84d7a31e4424cf65e390528698c19644075f3517bf2dc37f0f8aede79fe61
7
+ data.tar.gz: 6c54a8435e28008f9796741c9d95b915936e4a30dc4b8aaa6292e460796192585f1bf468b6845138d70630d8dd6052e6ce4f59d695a2988c7f252689402c1748
data/.gitignore ADDED
@@ -0,0 +1,30 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ #various artifacts
12
+ spec/examples.txt
13
+
14
+ # scm revert files
15
+ **.orig
16
+
17
+ # Mac finder artifacts
18
+ .DS_Store
19
+
20
+ # Netbeans project directory
21
+ /nbproject/
22
+
23
+ # RubyMine project files
24
+ .idea
25
+
26
+ # Textmate project files
27
+ /*.tmproj
28
+
29
+ # vim artifacts
30
+ **.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_attr_extended.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jason Haruska
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,260 @@
1
+ # ActiveAttrExtended
2
+
3
+ [![Build History][travis badge]][travis]
4
+ [![Code Climate][codeclimate badge]][codeclimate]
5
+
6
+ ActiveAttrExtended is an extension of ActiveAttr to include Arrays and Hashes as
7
+ valid attribute types.
8
+
9
+ ActiveAttr is a set of modules that makes it easy to create plain old ruby
10
+ models with functionality found in ORMs, like ActiveRecord, without
11
+ reinventing the wheel. Think of ActiveAttr as the stuff ActiveModel left out.
12
+
13
+ ## ActiveAttr vs ActiveAttrExtended
14
+
15
+ ActiveAttrExtended is a superset of functionality of ActiveAttr. The gem versions
16
+ in ActiveAttrExtended match the sister version in ActiveAttr where possible.
17
+
18
+ The biggest difference is the ability to use Arrays and Hashes as attribute types.
19
+
20
+ * [ActiveAttr API Documentation][aa_api]
21
+ * [Extended API Documentation][api]
22
+
23
+ [api]: http://rubydoc.info/gems/active_attr_extended
24
+ [aa_api]: http://rubydoc.info/gems/active_attr
25
+ [codeclimate badge]: https://codeclimate.com/github/haruska/active_attr_extended.png
26
+ [codeclimate]: https://codeclimate.com/github/haruska/active_attr_extended
27
+ [rubygems]: http://rubygems.org/gems/active_attr_extended
28
+ [protected_attributes]: https://github.com/rails/protected_attributes
29
+ [strong_parameters]: https://github.com/rails/strong_parameters
30
+ [travis badge]: https://secure.travis-ci.org/haruska/active_attr_extended.png?branch=master
31
+ [travis]: http://travis-ci.org/haruska/active_attr_extended
32
+
33
+ ## Modules ##
34
+
35
+ ### Attributes ###
36
+
37
+ Including the Attributes module into your class gives you a DSL for defining
38
+ the attributes of your model.
39
+
40
+ class Person
41
+ include ActiveAttr::Attributes
42
+
43
+ attribute :first_name
44
+ attribute :last_name
45
+ end
46
+
47
+ person = Person.new
48
+ person.first_name = "Chris"
49
+ person.last_name = "Griego"
50
+ person.attributes #=> {"first_name"=>"Chris", "last_name"=>"Griego"}
51
+
52
+ #### AttributeDefaults ####
53
+
54
+ Including the AttributeDefaults module into your class builds on Attributes by
55
+ allowing defaults to be declared with attributes.
56
+
57
+ class Person
58
+ include ActiveAttr::AttributeDefaults
59
+
60
+ attribute :first_name, :default => "John"
61
+ attribute :last_name, :default => "Doe"
62
+ end
63
+
64
+ person = Person.new
65
+ person.first_name #=> "John"
66
+ person.last_name #=> "Doe"
67
+
68
+ #### QueryAttributes ####
69
+
70
+ Including the QueryAttributes module into your class builds on Attributes by
71
+ providing instance methods for querying your attributes.
72
+
73
+ class Person
74
+ include ActiveAttr::QueryAttributes
75
+
76
+ attribute :first_name
77
+ attribute :last_name
78
+ end
79
+
80
+ person = Person.new
81
+ person.first_name = "Chris"
82
+ person.first_name? #=> true
83
+ person.last_name? #=> false
84
+
85
+ #### TypecastedAttributes ####
86
+
87
+ Including the TypecastedAttributes module into your class builds on Attributes
88
+ by providing type conversion for your attributes.
89
+
90
+ class Person
91
+ include ActiveAttr::TypecastedAttributes
92
+ attribute :age, :type => Integer
93
+ attribute :family, :type => Array
94
+ end
95
+
96
+ person = Person.new
97
+ person.age = "29"
98
+ person.age #=> 29
99
+ person.family = ["Peter", "Molly"]
100
+ person.family #=> ["Peter", "Molly]
101
+
102
+ ### BasicModel ###
103
+
104
+ Including the BasicModel module into your class gives you the bare minimum
105
+ required for your model to meet the ActiveModel API requirements.
106
+
107
+ class Person
108
+ include ActiveAttr::BasicModel
109
+ end
110
+
111
+ Person.model_name.plural #=> "people"
112
+ person = Person.new
113
+ person.valid? #=> true
114
+ person.errors.full_messages #=> []
115
+
116
+ ### BlockInitialization ###
117
+
118
+ Including the BlockInitialization module into your class will yield the model
119
+ instance to a block passed to when creating a new instance.
120
+
121
+ class Person
122
+ include ActiveAttr::BlockInitialization
123
+ attr_accessor :first_name, :last_name
124
+ end
125
+
126
+ person = Person.new do |p|
127
+ p.first_name = "Chris"
128
+ p.last_name = "Griego"
129
+ end
130
+
131
+ person.first_name #=> "Chris"
132
+ person.last_name #=> "Griego"
133
+
134
+ ### Logger ###
135
+
136
+ Including the Logger module into your class will give you access to a
137
+ configurable logger in model classes and instances. Your preferred logger can
138
+ be configured on an instance, subclass, class, parent class, and globally by
139
+ setting ActiveAttr::Logger.logger. When using Rails, the Rails framework
140
+ logger will be configured by default.
141
+
142
+ class Person
143
+ include ActiveAttr::Logger
144
+ end
145
+
146
+ Person.logger = Logger.new(STDOUT)
147
+ Person.logger? #=> true
148
+ Person.logger.info "Logging an informational message"
149
+
150
+ person = Person.new
151
+ person.logger? #=> true
152
+ person.logger = Logger.new(STDERR)
153
+ person.logger.warn "Logging a warning message"
154
+
155
+ ### MassAssignment ###
156
+
157
+ Including the MassAssignment module into your class gives you methods for bulk
158
+ initializing and updating the attributes of your model. Any unknown attributes
159
+ are silently ignored.
160
+
161
+ class Person
162
+ include ActiveAttr::MassAssignment
163
+ attr_accessor :first_name, :last_name, :age
164
+ end
165
+
166
+ person = Person.new(:first_name => "Christopher", :last_name => "Griego")
167
+ person.attributes = { :first_name => "Chris", :age => 21 }
168
+ person.first_name #=> "Chris"
169
+ person.last_name #=> "Griego"
170
+
171
+ MassAssignment supports mass assignment security/sanitization if a sanitizer
172
+ is included in the model. If using Rails 4.0, include ActiveModel's forbidden
173
+ attributes protection module to get support for strong parameters.
174
+
175
+ class Person
176
+ include ActiveAttr::MassAssignment
177
+ include ActiveModel::ForbiddenAttributesProtection
178
+ attr_accessor :first_name, :last_name
179
+ end
180
+
181
+ person = Person.new(ActionController::Parameters.new({
182
+ :first_name => "Chris",
183
+ :last_name => "Griego",
184
+ }).permit(:first_name))
185
+ person.first_name #=> "Chris"
186
+ person.last_name #=> nil
187
+
188
+ If using Rails 3.x or the [Protected Attributes gem][protected_attributes],
189
+ include ActiveModel's mass assignment security module to get support for
190
+ protected attributes, including support for mass assignment roles.
191
+
192
+ class Person
193
+ include ActiveAttr::MassAssignment
194
+ include ActiveModel::MassAssignmentSecurity
195
+ attr_accessor :first_name, :last_name
196
+ attr_protected :last_name
197
+ end
198
+
199
+ person = Person.new(:first_name => "Chris", :last_name => "Griego")
200
+ person.first_name #=> "Chris"
201
+ person.last_name #=> nil
202
+
203
+ If using the [Strong Parameters gem][strong_parameters] with Rails 3.2,
204
+ include the forbidden attributes protection module after including
205
+ the mass assignment security module.
206
+
207
+ class Person
208
+ include ActiveAttr::MassAssignment
209
+ include ActiveModel::MassAssignmentSecurity
210
+ include ActiveModel::ForbiddenAttributesProtection
211
+ end
212
+
213
+ ### Serialization ###
214
+
215
+ The Serialization module is a shortcut for incorporating ActiveModel's
216
+ serialization functionality into your model with one include.
217
+
218
+ class Person
219
+ include ActiveAttr::Serialization
220
+ end
221
+
222
+ ### Model ###
223
+
224
+ The Model module is a shortcut for incorporating the most common model
225
+ functionality into your model with one include. All of the above modules
226
+ are included when you include Model.
227
+
228
+ class Person
229
+ include ActiveAttr::Model
230
+ end
231
+
232
+ ## Integrations ##
233
+
234
+ ### Ruby on Rails ###
235
+
236
+ When using ActiveAttrExtended inside a Rails application, ActiveAttrExtended will configure
237
+ your models' default logger to use the Rails logger automatically. Just
238
+ include ActiveAttrExtended in your Gemfile.
239
+
240
+ gem "active_attr_extended"
241
+
242
+ ### RSpec ###
243
+
244
+ ActiveAttr comes with matchers and RSpec integration to assist you in testing
245
+ your models. The matchers also work with compatible frameworks like Shoulda.
246
+
247
+ require "active_attr/rspec"
248
+
249
+ describe Person do
250
+ it { is.expected_to have_attribute(:first_name).of_type(String).with_default_value_of("John") }
251
+ end
252
+
253
+ ## Contributing
254
+
255
+ Bug reports and pull requests are welcome on GitHub at https://github.com/haruska/active_attr_extended.
256
+
257
+ ## License
258
+
259
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
260
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.rspec_opts = "--format progress"
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_attr_extended/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_attr_extended"
8
+ spec.version = ActiveAttrExtended::VERSION
9
+ spec.authors = ["Jason Haruska"]
10
+ spec.email = ["jason@haruska.com"]
11
+
12
+ spec.summary = "What ActiveModel left out (including Collections)"
13
+ spec.description = "An extension of the active_attr gem, adding Arrays and Hashes as valid attribute types. Create plain old ruby models without reinventing the wheel."
14
+ spec.homepage = "https://github.com/haruska/active_attr_extended"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "active_attr", ">= 0.6.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_attr_extended"
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 'json'
2
+
3
+ module ActiveAttrExtended
4
+ module Typecasting
5
+ # Typecasts an Object to an Array
6
+ #
7
+ # @example Usage
8
+ # ArrayTypecaster.new.call(1) # => [1]
9
+ #
10
+ # @since 0.6.0
11
+ class ArrayTypecaster
12
+ # Typecasts an Object to an Array
13
+ #
14
+ # Will typecast any Object except nil to an Array, first by attempting to
15
+ # call `#to_a`, then by wrapping `value` in an Array (`[value]`).
16
+ #
17
+ # @example Usage
18
+ # ArrayTypecaster.new.call(1) # => [1]
19
+ #
20
+ # @param [Object] value The object to typecast
21
+ #
22
+ # @return [Array, nil] The result of typecasting
23
+ #
24
+ # @since 0.6.0
25
+ def call(value)
26
+ #treat incoming strings as serialized JSON
27
+ value = JSON::parse(value) if value.is_a? String
28
+
29
+ if value.nil?
30
+ nil
31
+ elsif value.respond_to? :to_a
32
+ value.to_a
33
+ else
34
+ [value]
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+
3
+ module ActiveAttrExtended
4
+ module Typecasting
5
+ # Typecasts an Object to a Hash
6
+ #
7
+ # @example Usage
8
+ # HashTypecaster.new.call(1) # => [1]
9
+ #
10
+ # @since 0.6.0
11
+ class HashTypecaster
12
+ # Typecasts an Object to a Hash
13
+ #
14
+ # Will typecast JSON or Hashes into a Hash
15
+ #
16
+ # @example Usage
17
+ # HashTypecaster.new.call(nil) # => {}
18
+ # HashTypecaster.new.call({:x => :y}) # => {:x => :y}
19
+ # HashTypecaster.new.call({:x => :y}.to_json) # => {:x => :y}
20
+ #
21
+ # @param [Object] value The object to typecast
22
+ #
23
+ # @return [Hash, nil] The result of typecasting
24
+ #
25
+ # @since 0.6.0
26
+ def call(value)
27
+ #treat incoming strings as serialized JSON
28
+ value = JSON::parse(value) if value.is_a? String
29
+ value.is_a?(Hash) ? value : {}
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveAttrExtended
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ require "active_attr"
2
+ require "active_attr_extended/version"
3
+ require "active_attr_extended/typecasting/array_typecaster"
4
+ require "active_attr_extended/typecasting/hash_typecaster"
5
+
6
+ module ActiveAttrExtended; end
7
+
8
+
9
+ # Reassign the typecaster mapping to include collections
10
+ ActiveAttr::Typecasting.module_eval { send(:remove_const, :TYPECASTER_MAP) }
11
+ module ActiveAttr
12
+ module Typecasting
13
+ # @private
14
+ TYPECASTER_MAP = {
15
+ Array => ::ActiveAttrExtended::Typecasting::ArrayTypecaster,
16
+ BigDecimal => BigDecimalTypecaster,
17
+ Boolean => BooleanTypecaster,
18
+ Date => DateTypecaster,
19
+ DateTime => DateTimeTypecaster,
20
+ Float => FloatTypecaster,
21
+ Hash => ::ActiveAttrExtended::Typecasting::HashTypecaster,
22
+ Integer => IntegerTypecaster,
23
+ Object => ObjectTypecaster,
24
+ String => StringTypecaster
25
+ }.freeze
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_attr_extended
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Haruska
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_attr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.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.6.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.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
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: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: An extension of the active_attr gem, adding Arrays and Hashes as valid
70
+ attribute types. Create plain old ruby models without reinventing the wheel.
71
+ email:
72
+ - jason@haruska.com
73
+ executables:
74
+ - console
75
+ - setup
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".ruby-version"
82
+ - ".travis.yml"
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - active_attr_extended.gemspec
88
+ - bin/console
89
+ - bin/setup
90
+ - lib/active_attr_extended.rb
91
+ - lib/active_attr_extended/typecasting/array_typecaster.rb
92
+ - lib/active_attr_extended/typecasting/hash_typecaster.rb
93
+ - lib/active_attr_extended/version.rb
94
+ homepage: https://github.com/haruska/active_attr_extended
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.5.1
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: What ActiveModel left out (including Collections)
118
+ test_files: []