hstore_attributes 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 396066b7fdc41c0c1039d85ebad2b42e5d04355b
4
+ data.tar.gz: eb91ce7217c92ef41ff6f953304da15d9143d31e
5
+ SHA512:
6
+ metadata.gz: 159f4b7ce845ba685475e02bf293422bc985cbcfc8a9da3655f6a5c74180d64114879cebaedb82c1381d0b92d0190feb3aaa2709623a141411de7df05dcae4df
7
+ data.tar.gz: b2bea8c6b9b38134e8d416ec5e1ce096738ee19989fe30aeb9385c170657951cc2fd24019c53893e793870dba450db9ff6f35dffd8934c233d845202ac5af862
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hstore_attributes.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kuba Kuźma
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,31 @@
1
+ # HstoreAttributes
2
+
3
+ Provides methods like `hstore_reader` or `hstore_writer` for accessing
4
+ your Hstore data like any other attributes. It also gives you a
5
+ possibility to typecast the value.
6
+
7
+ ## Usage
8
+
9
+ class Product < ActiveRecord::Base
10
+ extend HstoreAttributes
11
+
12
+ # The hstore column is named 'data'
13
+ hstore_accessor :data, :description
14
+ hstore_accessor :data, :price, :tax, type_cast: :to_i
15
+ hstore_accessor :data, :available_from, type_cast: ->(value) { value.to_s.to_date }
16
+ end
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ gem "hstore_attributes"
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install hstore_attributes
31
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << "lib" << "test"
7
+ test.pattern = "test/**/*_test.rb"
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hstore_attributes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hstore_attributes"
8
+ spec.version = HstoreAttributes::VERSION
9
+ spec.authors = ["Kuba Kuźma"]
10
+ spec.email = ["kuba@jah.pl"]
11
+ spec.description = %q{Provides methods like hstore_reader or hstore_writer for accessing your Hstore data like any other attributes.}
12
+ spec.summary = %q{Easy accessors for Hstore data.}
13
+ spec.homepage = "https://github.com/qoobaa/hstore_attributes"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,3 @@
1
+ module HstoreAttributes
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require "hstore_attributes/version"
2
+
3
+ module HstoreAttributes
4
+ def hstore_reader(hstore, *symbols)
5
+ options = symbols.extract_options!
6
+ symbols.each do |symbol|
7
+ define_method(symbol) do
8
+ value = (send(hstore) || {})[symbol.to_s]
9
+ value = options[:type_cast].to_proc.call(value) if options[:type_cast]
10
+ value
11
+ end
12
+ end
13
+ end
14
+
15
+ def hstore_writer(hstore, *symbols)
16
+ options = symbols.extract_options!
17
+ symbols.each do |symbol|
18
+ define_method("#{symbol}=") do |value|
19
+ value = options[:type_cast].to_proc.call(value) if options[:type_cast]
20
+ send("#{hstore}=", (send(hstore) || {}).merge(symbol.to_s => value.to_s))
21
+ end
22
+ end
23
+ end
24
+
25
+ def hstore_accessor(hstore, *symbols)
26
+ hstore_reader(hstore, *symbols)
27
+ hstore_writer(hstore, *symbols)
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+ class Product < DummyModel
4
+ extend HstoreAttributes
5
+
6
+ hstore_accessor :data, :name, :description, type_cast: ->(value) { value.to_s.upcase }
7
+ end
8
+
9
+ describe "hstore_writer" do
10
+ def setup
11
+ @product = Product.new
12
+ end
13
+
14
+ it "sets and gets name properly" do
15
+ @product.name = "zomg"
16
+ assert_equal "ZOMG", @product.name
17
+ end
18
+
19
+ it "sets and gets description properly" do
20
+ @product.description = "trolololo"
21
+ assert_equal "TROLOLOLO", @product.description
22
+ end
23
+ end
@@ -0,0 +1,70 @@
1
+ require "test_helper"
2
+
3
+ class Product < DummyModel
4
+ extend HstoreAttributes
5
+
6
+ hstore_reader :data, :price, :price_2, type_cast: :to_i
7
+ hstore_reader :data, :name, type_cast: ->(value) { value.to_s.upcase }
8
+ hstore_reader :data, :description
9
+
10
+ def data
11
+ @data
12
+ end
13
+
14
+ def data=(data)
15
+ @data = data
16
+ end
17
+ end
18
+
19
+ describe "hstore_reader" do
20
+ def setup
21
+ @product = Product.new
22
+ end
23
+
24
+ it "returns price 0 if no hstore" do
25
+ assert_equal 0, @product.price
26
+ end
27
+
28
+ it "returns price 0 if no price in hstore" do
29
+ @product.data = {}
30
+ assert_equal 0, @product.price
31
+ end
32
+
33
+ it "returns price if already in hstore" do
34
+ @product.data = {"price" => "100"}
35
+ assert_equal 100, @product.price
36
+ end
37
+
38
+ it "returns price_2 if already in hstore" do
39
+ @product.data = {"price" => 100, "price_2" => "500"}
40
+ assert_equal 500, @product.price_2
41
+ end
42
+
43
+ it "returns description nil if no hstore" do
44
+ assert_equal nil, @product.description
45
+ end
46
+
47
+ it "returns description nil if description in hstore" do
48
+ @product.data = {}
49
+ assert_equal nil, @product.description
50
+ end
51
+
52
+ it "returns description if already in hstore" do
53
+ @product.data = {"description" => "zomg"}
54
+ assert_equal "zomg", @product.description
55
+ end
56
+
57
+ it "returns empty name if no hstore" do
58
+ assert_equal "", @product.name
59
+ end
60
+
61
+ it "returns empty name if name in hstore" do
62
+ @product.data = {}
63
+ assert_equal "", @product.name
64
+ end
65
+
66
+ it "returns upcased name if already in hstore" do
67
+ @product.data = {"name" => "zomg"}
68
+ assert_equal "ZOMG", @product.name
69
+ end
70
+ end
@@ -0,0 +1,51 @@
1
+ require "test_helper"
2
+
3
+ class Product < DummyModel
4
+ extend HstoreAttributes
5
+
6
+ hstore_writer :data, :name, :name_2
7
+ hstore_writer :data, :price, type_cast: :to_i
8
+ hstore_writer :data, :cents, type_cast: ->(value) { (value.to_f * 100).to_i }
9
+ end
10
+
11
+ describe "hstore_writer" do
12
+ def setup
13
+ @product = Product.new
14
+ end
15
+
16
+ it "sets name if no hstore" do
17
+ @product.name = "zomg"
18
+ assert_equal({"name" => "zomg"}, @product.data)
19
+ end
20
+
21
+ it "sets name if empty hstore" do
22
+ hstore = {}
23
+ @product.data = hstore
24
+ @product.name = "zomg"
25
+ assert_equal({"name" => "zomg"}, @product.data)
26
+ refute_same hstore, @product.data
27
+ end
28
+
29
+ it "sets name if already in hstore" do
30
+ hstore = {"name" => "trolololo"}
31
+ @product.data = hstore
32
+ @product.name = "zomg"
33
+ assert_equal({"name" => "zomg"}, @product.data)
34
+ refute_same hstore, @product.data
35
+ end
36
+
37
+ it "sets name_2 if no hstore" do
38
+ @product.name_2 = "lolo"
39
+ assert_equal({"name_2" => "lolo"}, @product.data)
40
+ end
41
+
42
+ it "sets price if no hstore" do
43
+ @product.price = 0
44
+ assert_equal({"price" => "0"}, @product.data)
45
+ end
46
+
47
+ it "sets cents using type_cast function" do
48
+ @product.cents = "1"
49
+ assert_equal({"cents" => "100"}, @product.data)
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ require "minitest/autorun"
2
+ require "active_support"
3
+ require "hstore_attributes"
4
+
5
+ # DummyModel emulates an ActiveRecord model with hstore "data"
6
+
7
+ class DummyModel
8
+ def data
9
+ @data
10
+ end
11
+
12
+ def data=(data)
13
+ @data = data
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hstore_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kuba Kuźma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Provides methods like hstore_reader or hstore_writer for accessing your
56
+ Hstore data like any other attributes.
57
+ email:
58
+ - kuba@jah.pl
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - hstore_attributes.gemspec
69
+ - lib/hstore_attributes.rb
70
+ - lib/hstore_attributes/version.rb
71
+ - test/hstore_accessor_test.rb
72
+ - test/hstore_reader_test.rb
73
+ - test/hstore_writer_test.rb
74
+ - test/test_helper.rb
75
+ homepage: https://github.com/qoobaa/hstore_attributes
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.0
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Easy accessors for Hstore data.
99
+ test_files:
100
+ - test/hstore_accessor_test.rb
101
+ - test/hstore_reader_test.rb
102
+ - test/hstore_writer_test.rb
103
+ - test/test_helper.rb