activerecord-hstore_properties 0.0.1.alpha
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +22 -0
- data/README.textile +76 -0
- data/Rakefile +5 -0
- data/activerecord-hstore_properties.gemspec +21 -0
- data/lib/active_record/hstore_properties.rb +66 -0
- data/lib/active_record/properties/base.rb +6 -0
- data/lib/active_record/properties/boolean_property.rb +9 -0
- data/lib/active_record/properties/counter_property.rb +9 -0
- data/lib/active_record/properties/extensions/booleans.rb +15 -0
- data/lib/active_record/properties/extensions/counters.rb +28 -0
- data/lib/active_record/properties/number_property.rb +9 -0
- data/lib/active_record/properties/string_property.rb +9 -0
- data/lib/activerecord-hstore_properties/version.rb +5 -0
- data/lib/activerecord-hstore_properties.rb +12 -0
- data/spec/spec_helper.rb +1 -0
- data/test/test_helper.rb +2 -0
- metadata +83 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT LICENSE
|
2
|
+
|
3
|
+
Copyright (c) stevo <blazejek@gmail.com>
|
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/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 stevo
|
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.textile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
h1. hstore-properties
|
2
|
+
|
3
|
+
|
4
|
+
h2. Setup
|
5
|
+
|
6
|
+
|
7
|
+
Simply add following to your gemfile
|
8
|
+
|
9
|
+
@gem 'hstore-properties'@
|
10
|
+
|
11
|
+
Create @properties@ column in the model you would like to use properties within, i.e.
|
12
|
+
|
13
|
+
@rails g migration AddPropertiesToUsers properties:hstore@
|
14
|
+
|
15
|
+
Apply your migration
|
16
|
+
|
17
|
+
@rake db:migrate@
|
18
|
+
|
19
|
+
Finally, describe your properties in your model file
|
20
|
+
|
21
|
+
bc. class User < ActiveRecord::Base
|
22
|
+
include ActiveRecord::Properties
|
23
|
+
properties 'third_name',
|
24
|
+
'some_cool_feature' => :boolean,
|
25
|
+
'comments' => :counter,
|
26
|
+
'age' => :number
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
h2. Usage
|
31
|
+
|
32
|
+
By default, all your properties are of type String. There are number of other property types available though...
|
33
|
+
|
34
|
+
# string
|
35
|
+
# boolean
|
36
|
+
# number
|
37
|
+
# counter
|
38
|
+
|
39
|
+
All properties can be retrieved just as they are written into hstore column, by suffixing them with @_property@, i.e.
|
40
|
+
|
41
|
+
@User.last.third_name_property #=> "Jack"@
|
42
|
+
|
43
|
+
*Boolean* properties, can be additionaly retrieved by using @_enabled?@ suffix, that will cast them to boolean value, i.e.
|
44
|
+
|
45
|
+
@User.last.some_cool_feature_enabled? #=> true@
|
46
|
+
|
47
|
+
*Counter* properties, can be retrieved by using @_count@ suffix, that will cast them to integer value, i.e.
|
48
|
+
|
49
|
+
@User.last.comments_count #=> 10@
|
50
|
+
|
51
|
+
What is more, it is possible to bump counter properties, i.e. following line will increment comments property by 1
|
52
|
+
|
53
|
+
@User.last.comments_bump!@
|
54
|
+
|
55
|
+
|
56
|
+
h2. Updating through forms
|
57
|
+
|
58
|
+
bc. <%= f.fields_for :properties, OpenStruct.new(@user.properties) do |builder| %>
|
59
|
+
<% User.properties.each do |property| %>
|
60
|
+
<%= builder.input property.name, property.formtastic_options %>
|
61
|
+
<% end %>
|
62
|
+
<% end %>
|
63
|
+
|
64
|
+
|
65
|
+
h2. Further customization
|
66
|
+
|
67
|
+
If most of your properties are of the same type, but other than string, you can overwrite @default_proeprty_klass@ to make other type default, i.e.
|
68
|
+
|
69
|
+
bc.
|
70
|
+
class User < ActiveRecord::Base
|
71
|
+
#...
|
72
|
+
def default_property_klass
|
73
|
+
ActiveRecord::Properties::BooleanProperty
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord-hstore_properties/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "activerecord-hstore_properties"
|
8
|
+
gem.version = Activerecord::HstoreProperties::VERSION
|
9
|
+
gem.authors = ["stevo"]
|
10
|
+
gem.email = ["blazej.kosmowski@selle.com "]
|
11
|
+
gem.homepage = "https://github.com//hstore-properties"
|
12
|
+
gem.summary = "Facilitate defining and retrieving information from hstore column"
|
13
|
+
gem.description = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require "active_record/properties/base"
|
2
|
+
require "active_record/properties/boolean_property"
|
3
|
+
require "active_record/properties/counter_property"
|
4
|
+
require "active_record/properties/number_property"
|
5
|
+
require "active_record/properties/string_property"
|
6
|
+
require "active_record/properties/extensions/booleans"
|
7
|
+
require "active_record/properties/extensions/counters"
|
8
|
+
|
9
|
+
module ActiveRecord
|
10
|
+
module HstoreProperties
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
included do
|
14
|
+
include ActiveRecord::Properties::Extensions::Booleans
|
15
|
+
include ActiveRecord::Properties::Extensions::Counters
|
16
|
+
|
17
|
+
serialize :properties, ::ActiveRecord::Coders::Hstore
|
18
|
+
class_attribute :_properties
|
19
|
+
end
|
20
|
+
|
21
|
+
def define_property_method(method_name, suffix, &block)
|
22
|
+
if method_name.ends_with?(suffix) and (self.class._properties.detect { |pn| pn.name.to_s == method_name.sub(suffix, '') })
|
23
|
+
prop_name = method_name.sub(suffix, '')
|
24
|
+
self.class.send(:define_method, method_name) do
|
25
|
+
block.call(prop_name, self)
|
26
|
+
end
|
27
|
+
[true, block.call(prop_name, self)]
|
28
|
+
else
|
29
|
+
[false, nil]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module ClassMethods
|
34
|
+
def properties(*args)
|
35
|
+
if args.blank?
|
36
|
+
self._properties ||= []
|
37
|
+
else
|
38
|
+
self._properties ||= []
|
39
|
+
|
40
|
+
new_properties = extract_properties(args)
|
41
|
+
self._properties += new_properties
|
42
|
+
|
43
|
+
new_properties.each do |property|
|
44
|
+
define_method("#{property.name.to_s}_property") { properties[property.name.to_s] }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def default_property_klass
|
50
|
+
ActiveRecord::Properties::StringProperty
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def extract_properties(args)
|
56
|
+
typed_properties = args.extract_options!
|
57
|
+
|
58
|
+
result = args.map { |property| default_property_klass.new(property) }
|
59
|
+
typed_properties.each do |property, _type|
|
60
|
+
result << "active_record/properties/#{_type.to_s}_property".camelize.constantize.new(property)
|
61
|
+
end
|
62
|
+
result
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Properties
|
3
|
+
module Extensions
|
4
|
+
module Booleans
|
5
|
+
def method_missing(*args, &block)
|
6
|
+
method_name = args.first.to_s
|
7
|
+
defined, result = define_property_method(method_name, '_enabled?') do |property_name, object|
|
8
|
+
::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(object.send("#{property_name}_property"))
|
9
|
+
end
|
10
|
+
defined ? result : super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Allows fetching properties as numerical values by adding _count suffix
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Properties
|
5
|
+
module Extensions
|
6
|
+
module Counters
|
7
|
+
def method_missing(*args, &block)
|
8
|
+
method_name = args.first.to_s
|
9
|
+
|
10
|
+
defined, result = if method_name.ends_with?('_bump!')
|
11
|
+
define_property_method(method_name, '_bump!') do |property_name, object|
|
12
|
+
_properties = object.properties
|
13
|
+
_properties[property_name] = object.send("#{property_name}_count") + 1
|
14
|
+
object.update_column(:properties, ActiveRecord::Coders::Hstore.new({}).dump(_properties))
|
15
|
+
_properties[property_name]
|
16
|
+
end
|
17
|
+
else
|
18
|
+
define_property_method(method_name, '_count') do |property_name, object|
|
19
|
+
object.send("#{property_name}_property").to_i
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
defined ? result : super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#
|
2
|
+
#require 'active_support'
|
3
|
+
#
|
4
|
+
#if defined? Rails
|
5
|
+
# require "activerecord-hstore_properties/railties"
|
6
|
+
#else
|
7
|
+
# ActiveSupport.on_load :active_record do
|
8
|
+
# require "activerecord-hstore_properties/activerecord"
|
9
|
+
# end
|
10
|
+
#end
|
11
|
+
require "activerecord-hstore_properties/version"
|
12
|
+
require "active_record/hstore_properties"
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'activerecord-hstore_properties'
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-hstore_properties
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.alpha
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- stevo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ''
|
31
|
+
email:
|
32
|
+
- ! 'blazej.kosmowski@selle.com '
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.textile
|
43
|
+
- Rakefile
|
44
|
+
- activerecord-hstore_properties.gemspec
|
45
|
+
- lib/active_record/hstore_properties.rb
|
46
|
+
- lib/active_record/properties/base.rb
|
47
|
+
- lib/active_record/properties/boolean_property.rb
|
48
|
+
- lib/active_record/properties/counter_property.rb
|
49
|
+
- lib/active_record/properties/extensions/booleans.rb
|
50
|
+
- lib/active_record/properties/extensions/counters.rb
|
51
|
+
- lib/active_record/properties/number_property.rb
|
52
|
+
- lib/active_record/properties/string_property.rb
|
53
|
+
- lib/activerecord-hstore_properties.rb
|
54
|
+
- lib/activerecord-hstore_properties/version.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
homepage: https://github.com//hstore-properties
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>'
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 1.3.1
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.24
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Facilitate defining and retrieving information from hstore column
|
81
|
+
test_files:
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
- test/test_helper.rb
|