hstore_accessor_moi_solutions 1.0.4

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
+ SHA256:
3
+ metadata.gz: 82efcb2320fb250311c3bf3b175a7cd00455b35d1d12746ce047e2eedab97d11
4
+ data.tar.gz: a8649bab653d1cd5232b5f74004161526f466294ba79567f1d02bd181cbbed27
5
+ SHA512:
6
+ metadata.gz: dd065f8068a438f2952ca8c98c8a8eacba68a0a37d6fa49151489008371af433f476eb43ab99405b66732ef4239a604d1af5810d87bb13db38ba5cdd8c67dab2
7
+ data.tar.gz: 3fe38adcda80a80b800ef4c985791b4c1c4641041e892c3a9f16ef16d04f112d797c672aea8d7b2ccd5908c0f6435bdaf611514721f955d0bbba9cdf909771e5
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ logfile
19
+ gemfiles/*.gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,43 @@
1
+ AllCops:
2
+ Include:
3
+ - Rakefile
4
+ Lint/SpaceBeforeFirstArg:
5
+ Enabled: false
6
+ Lint/UnusedBlockArgument:
7
+ Enabled: false
8
+ Lint/UnusedMethodArgument:
9
+ Enabled: false
10
+ Metrics/AbcSize:
11
+ Enabled: false
12
+ Metrics/ClassLength:
13
+ Enabled: false
14
+ Metrics/CyclomaticComplexity:
15
+ Enabled: false
16
+ Metrics/LineLength:
17
+ Enabled: false
18
+ Metrics/MethodLength:
19
+ Enabled: false
20
+ Metrics/PerceivedComplexity:
21
+ Enabled: false
22
+ Style/AlignParameters:
23
+ Enabled: false
24
+ Style/ClassAndModuleChildren:
25
+ Enabled: false
26
+ Style/ClassVars:
27
+ Enabled: false
28
+ Style/Documentation:
29
+ Enabled: false
30
+ Style/FileName:
31
+ Enabled: false
32
+ Style/GuardClause:
33
+ Enabled: false
34
+ Style/IndentHash:
35
+ Enabled: false
36
+ Style/RescueModifier:
37
+ Enabled: false
38
+ Style/SignalException:
39
+ Enabled: false
40
+ Style/SpaceAroundEqualsInParameterDefault:
41
+ Enabled: false
42
+ Style/StringLiterals:
43
+ EnforcedStyle: double_quotes
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.5
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ appraise "activerecord-4.2" do
2
+ gem "activerecord", "4.2.0"
3
+ end
4
+
5
+ appraise "activerecord-4.1" do
6
+ gem "activerecord", "4.1.0"
7
+ end
8
+
9
+ appraise "activerecord-4.0" do
10
+ gem "activerecord", "4.0.0"
11
+ end
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem "pg", ">= 0.14.1"
7
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 JC Grubbs
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,290 @@
1
+ # HstoreAccessor
2
+
3
+ Hstore Accessor allows you to treat fields on an hstore column as though they were actual columns being picked up by ActiveRecord. This is especially handy when trying to avoid sparse columns while making use of [single table inheritence](#single-table-inheritance). Hstore Accessor currently supports ActiveRecord versions 4.0, 4.1, and 4.2.
4
+
5
+ Not a fan of hstore? Why not check out [Jsonb Accessor](https://github.com/devmynd/jsonb_accessor)?
6
+
7
+ ## Table of Contents
8
+
9
+ * [Installation](#installation)
10
+ * [Setup](#setup)
11
+ * [ActiveRecord methods generated for fields](#activerecord-methods-generated-for-fields)
12
+ * [Scopes](#scopes)
13
+ * [String Fields](#string-fields)
14
+ * [Integer, Float, and Decimal Fields](#integer-float-decimal-fields)
15
+ * [Datetime Fields](#datetime-fields)
16
+ * [Date Fields](#date-fields)
17
+ * [Array Fields](#array-fields)
18
+ * [Boolean Fields](#boolean-fields)
19
+ * [Single Table Inheritence](#single-table-inheritance)
20
+ * [Upgrading](#upgrading)
21
+ * [Contributing](#contributing)
22
+ - [Basics](#basics)
23
+ - [Developing Locally](#developing-locally)
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem "hstore_accessor"
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install hstore_accessor
40
+
41
+ ## Setup
42
+
43
+ The `hstore_accessor` method accepts the name of the hstore column you'd
44
+ like to use and a hash with keys representing fields and values
45
+ indicating the type to be stored in that field. The available types
46
+ are: `string`, `integer`, `float`, `decimal`, `datetime`, `date`, `boolean`, `array`, and `hash`. It is available on an class that inherits from `ActiveRecord::Base`.
47
+
48
+ ```ruby
49
+ class Product < ActiveRecord::Base
50
+ hstore_accessor :options,
51
+ color: :string,
52
+ weight: :integer,
53
+ price: :float,
54
+ built_at: :datetime,
55
+ build_date: :date,
56
+ tags: :array, # deprecated
57
+ ratings: :hash # deprecated
58
+ miles: :decimal
59
+ end
60
+ ```
61
+
62
+ Now you can interact with the fields stored in the hstore directly.
63
+
64
+ ```ruby
65
+ product = Product.new
66
+ product.color = "green"
67
+ product.weight = 34
68
+ product.price = 99.95
69
+ product.built_at = Time.now - 10.days
70
+ product.build_date = Date.today
71
+ product.popular = true
72
+ product.tags = %w(housewares kitchen) # deprecated
73
+ product.ratings = { user_a: 3, user_b: 4 } # deprecated
74
+ product.miles = 3.14
75
+ ```
76
+
77
+ Reading these fields works as well.
78
+
79
+ ```ruby
80
+ product.color # => "green"
81
+ product.price # => 99.95
82
+ ```
83
+
84
+ In order to reduce the storage overhead of hstore keys (especially when
85
+ indexed) you can specify an alternate key.
86
+
87
+ ```ruby
88
+ hstore_accessor :options,
89
+ color: { data_type: :string, store_key: "c" },
90
+ weight: { data_type: :integer, store_key: "w" }
91
+ ```
92
+
93
+ In the above example you can continue to interact with the fields using
94
+ their full name but when saved to the database the field will be set
95
+ using the `store_key`.
96
+
97
+ Additionally, dirty tracking is implemented in the same way that normal
98
+ `ActiveRecord` fields work.
99
+
100
+ ```ruby
101
+ product.color #=> "green"
102
+ product.color = "blue"
103
+ product.changed? #=> true
104
+ product.color_changed? #=> true
105
+ product.color_was #=> "green"
106
+ product.color_change #=> ["green", "blue"]
107
+ ```
108
+
109
+ ## ActiveRecord methods generated for fields
110
+
111
+ ```ruby
112
+ class Product < ActiveRecord::Base
113
+ hstore_accessor :data, field: :string
114
+ end
115
+ ```
116
+
117
+ * `field`
118
+ * `field=`
119
+ * `field?`
120
+ * `field_changed?`
121
+ * `field_was`
122
+ * `field_change`
123
+ * `reset_field!`
124
+ * `restore_field!`
125
+ * `field_will_change!`
126
+
127
+ Overriding methods is supported, with access to the original Hstore Accessor implementation available via `super`.
128
+
129
+ Additionally, there is also `hstore_metadata_for_<fields>` on both the class and instances. `column_for_attribute` will also return a column object for an Hstore Accessor defined field. If you're using ActiveRecord 4.2, `type_for_attribute` will return a type object for Hstore Accessor defined fields the same as it does for actual columns.
130
+
131
+ ## Scopes
132
+
133
+ The `hstore_accessor` macro also creates scopes for `string`, `integer`,
134
+ `float`, `decimal`, `time`, `date`, `boolean`, and `array` fields.
135
+
136
+ ### String Fields
137
+
138
+ For `string` types, a `with_<key>` scope is created which checks for
139
+ equality.
140
+
141
+ ```ruby
142
+ Product.with_color("green")
143
+ ```
144
+
145
+ ### Integer, Float, Decimal Fields
146
+
147
+ For `integer`, `float` and `decimal` types five scopes are created:
148
+
149
+ ```ruby
150
+ Product.price_lt(240.00) # price less than
151
+ Product.price_lte(240.00) # price less than or equal to
152
+ Product.price_eq(240.00) # price equal to
153
+ Product.price_gte(240.00) # price greater than or equal to
154
+ Product.price_gt(240.00) # price greater than
155
+ ```
156
+
157
+ ### Datetime Fields
158
+
159
+ For `datetime` fields, three scopes are created:
160
+
161
+ ```ruby
162
+ Product.built_at_before(Time.now) # built before the given time
163
+ Product.built_at_eq(Time.now - 10.days) # built at an exact time
164
+ Product.built_at_after(Time.now - 4.days) # built after the given time
165
+ ```
166
+
167
+ ### Date Fields
168
+
169
+ For `date` fields, three scopes are created:
170
+
171
+ ```ruby
172
+ Product.build_date_before(Date.today) # built before the given date
173
+ Product.build_date_eq(Date.today - 10.days) # built at an exact date
174
+ Product.built_date_after(Date.today - 4.days) # built after the given date
175
+ ```
176
+
177
+ ### Array Fields
178
+
179
+ *Note: the array field type is deprecated. It is available in version 0.9.0 but not > 1.0.0*
180
+
181
+ For `array` types, two scopes are created:
182
+
183
+ ```ruby
184
+ Product.tags_eq(%w(housewares kitchen)) # tags equaling
185
+ Product.tags_contains("kitchen") # tags containing a single value
186
+ Product.tags_contains(%w(housewares kitchen)) # tags containing a number of values
187
+ ```
188
+
189
+ ### Boolean Fields
190
+
191
+ Two scopes are created for `boolean` fields:
192
+
193
+ ```ruby
194
+ Product.is_popular # => when popular is set to true
195
+ Product.not_popular # => when popular is set to false
196
+ ```
197
+
198
+ Predicate methods are also available on instances:
199
+
200
+ ```ruby
201
+ product = Product.new(popular: true)
202
+ product.popular? # => true
203
+ ```
204
+
205
+ ### Single-table Inheritance
206
+
207
+ One of the big issues with `ActiveRecord` single-table inheritance (STI)
208
+ is sparse columns. Essentially, as sub-types of the original table
209
+ diverge further from their parent more columns are left empty in a given
210
+ table. Postgres' `hstore` type provides part of the solution in that
211
+ the values in an `hstore` column does not impose a structure - different
212
+ rows can have different values.
213
+
214
+ We set up our table with an hstore field:
215
+
216
+ ```ruby
217
+ # db/migration/<timestamp>_create_players_table.rb
218
+ class CreateVehiclesTable < ActiveRecord::Migration
219
+ def change
220
+ create_table :vehicles do |t|
221
+ t.string :make
222
+ t.string :model
223
+ t.integer :model_year
224
+ t.string :type
225
+ t.hstore :data
226
+ end
227
+ end
228
+ end
229
+ ```
230
+
231
+ And for our models:
232
+
233
+ ```ruby
234
+ # app/models/vehicle.rb
235
+ class Vehicle < ActiveRecord::Base
236
+ end
237
+
238
+ # app/models/vehicles/automobile.rb
239
+ class Automobile < Vehicle
240
+ hstore_accessor :data,
241
+ axle_count: :integer,
242
+ weight: :float,
243
+ engine_details: :hash
244
+ end
245
+
246
+ # app/models/vehicles/airplane.rb
247
+ class Airplane < Vehicle
248
+ hstore_accessor :data,
249
+ engine_type: :string,
250
+ safety_rating: :integer,
251
+ features: :hash
252
+ end
253
+ ```
254
+
255
+ From here any attributes specific to any sub-class can be stored in the
256
+ `hstore` column avoiding sparse data. Indices can also be created on
257
+ individual fields in an `hstore` column.
258
+
259
+ This approach was originally concieved by Joe Hirn in [this blog
260
+ post](http://www.devmynd.com/blog/2013-3-single-table-inheritance-hstore-lovely-combination).
261
+
262
+ ## Upgrading
263
+ Upgrading from version 0.6.0 to 0.9.0 should be fairly painless. If you were previously using a `time` type fields, simply change it to `datetime` like so:
264
+
265
+ ```ruby
266
+ # Before...
267
+ hstore_accessor :data, published_at: :time
268
+ # After...
269
+ hstore_accessor :data, published_at: :datetime
270
+ ```
271
+
272
+ While the `array` and `hash` types are available in version 0.9.0, they are deprecated and are not available in 1.0.0.
273
+
274
+ ## Contributing
275
+ ### Basics
276
+ 1. [Fork it](https://github.com/devmynd/hstore_accessor/fork)
277
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
278
+ 3. Write code _and_ tests
279
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
280
+ 5. Push to the branch (`git push origin my-new-feature`)
281
+ 6. Create new Pull Request
282
+
283
+ ### Developing Locally
284
+ Before you make your pull requests, please make sure you style is in line with our Rubocop settings and that all of the tests pass.
285
+
286
+ 1. `bundle install`
287
+ 2. `appraisal install`
288
+ 3. Make sure Postgres is installed and running
289
+ 4. `appraisal rspec` to run all the tests
290
+ 5. `rubocop` to check for style
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new
8
+ RuboCop::RakeTask.new
9
+
10
+ task(default: [:rubocop, :spec])
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.0.0"
6
+
7
+ group :test do
8
+ gem "pg", ">= 0.14.1"
9
+ end
10
+
11
+ gemspec :path => "../"
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.1.0"
6
+
7
+ group :test do
8
+ gem "pg", ">= 0.14.1"
9
+ end
10
+
11
+ gemspec :path => "../"