active_store_accessor 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: 204a4d2653c210af6ecf37c728933d00412a0523
4
+ data.tar.gz: d37a87d43c4d2ce8fe53b065ad3a9639ce651e60
5
+ SHA512:
6
+ metadata.gz: ef4651493e7063e99909b7d147a6cc3acbd0a34216c0ee6f653e72e4f5a8eeb6f48a6a3a21196dd639a08fbaa218284a6f81b2a38aa1ec65593b18365b8a0ecb
7
+ data.tar.gz: e9d4a3132b22cffe3298d0e6ceddd163250ad376dfe41dd370e1a247a5ce19bbe36271f36bd1088da7479888a8a41f05f302d201c662fbd4c0f7a03f1b6831a1
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ env:
2
+ - "RAILS_VERSION=3.2"
3
+ - "RAILS_VERSION=4.0"
4
+ - "RAILS_VERSION=master"
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - 2.1.2
9
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_store_accessor.gemspec
4
+ gemspec
5
+
6
+ ar_version = ENV["AR_VERSION"]
7
+ ar_version = case ar_version
8
+ when "3.2" then "~> 3.2.0"
9
+ when "4.0" then "~> 4.0.0"
10
+ else
11
+ "~> 4.1.0"
12
+ end
13
+ gem "activerecord", ar_version
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sergey Pchelincev
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,35 @@
1
+ # ActiveStoreAccessor
2
+
3
+ Get more from ActiveRecord::Store
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_store_accessor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_store_accessor
18
+
19
+ ## Usage
20
+
21
+ After you add gem into Gemfile everything is done for you. Now you can declare your serialized properties in a next way:
22
+
23
+ # basic usage(where `info` is a store column)
24
+ active_store_accessor :info, age: :integer, birthday: :time
25
+
26
+ # with default values
27
+ active_store_accessor :info, score: { type: :float, default: 0.0 }, active: { type: :boolean, default: true }
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/[my-github-username]/active_store_accessor/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "lib"
6
+ t.libs << "tests"
7
+ t.pattern = 'tests/**/*_test.rb'
8
+ end
9
+
10
+ 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
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "active_store_accessor"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Sergey Pchelincev"]
9
+ spec.email = ["mail@sergeyp.me"]
10
+ spec.summary = %q{Get more from ActiveRecord::Store}
11
+ spec.description = spec.summary
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activerecord", ">= 3.2"
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest", ">= 4.7"
24
+ end
@@ -0,0 +1,31 @@
1
+ module ActiveStoreAccessor
2
+ def active_store_attributes
3
+ return @active_store_attributes if @active_store_attributes
4
+
5
+ @active_store_attributes = superclass.respond_to?(:active_store_attributes) ? superclass.active_store_attributes : {}
6
+ end
7
+
8
+ def active_store_accessor(column_name, attrs)
9
+ store column_name, accessors: attrs.keys
10
+ attrs.each do |attr_name, options|
11
+ options = { type: options.to_s } unless options.is_a?(Hash)
12
+ type = options.fetch(:type) { raise ArgumentError, "please specify type of attribute" }.to_s
13
+ args = [attr_name.to_s, options[:default], type]
14
+ active_store_attributes[attr_name] = ActiveRecord::ConnectionAdapters::Column.new(*args)
15
+
16
+ class_eval <<-RUBY
17
+ def #{ attr_name }
18
+ column = self.class.active_store_attributes[:#{ attr_name }]
19
+ value = column.type_cast(super)
20
+ value.nil? ? column.default : value
21
+ end
22
+
23
+ def #{ attr_name }=(value)
24
+ super self.class.active_store_attributes[:#{ attr_name }].type_cast_for_write(value)
25
+ end
26
+ RUBY
27
+ end
28
+ end
29
+ end
30
+
31
+ ActiveSupport.on_load(:active_record) { ActiveRecord::Base.extend(ActiveStoreAccessor) }
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ describe ActiveStoreAccessor do
4
+ it "should support common types" do
5
+ profile = Profile.new
6
+
7
+ assert_equal profile.age, nil
8
+ assert_equal profile.score, 10
9
+ assert_equal profile.rank, nil
10
+ assert_equal profile.birthday, nil
11
+ assert_equal profile.confirmed, nil
12
+
13
+ profile.age = "20"
14
+ profile.score = 100.32
15
+ profile.rank = "3213.312"
16
+ profile.birthday = Time.utc(2014, 5, 12)
17
+ profile.confirmed = "1"
18
+
19
+ assert_equal profile.age, 20
20
+ assert_equal profile.score, 100
21
+ assert_equal profile.rank, 3213.312
22
+ assert_equal profile.birthday, Time.utc(2014, 5, 12)
23
+ assert_equal profile.confirmed, true
24
+ end
25
+
26
+ it "should support model inheritance" do
27
+ admin_profile = AdminProfile.new(age: 23, level: 5)
28
+
29
+ assert_equal admin_profile.age, 23
30
+ assert_equal admin_profile.level, 5
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ require "minitest/autorun"
2
+ require "active_record"
3
+ require "active_store_accessor"
4
+
5
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
6
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
7
+
8
+ ActiveRecord::Schema.define do
9
+ create_table :profiles do |t|
10
+ t.text :info
11
+ end
12
+ end
13
+
14
+ class Profile < ActiveRecord::Base
15
+ active_store_accessor :info, age: :integer, score: { type: :integer, default: 10 }
16
+ active_store_accessor :info, rank: :float
17
+ active_store_accessor :info, birthday: :time
18
+ active_store_accessor :info, confirmed: :boolean
19
+ end
20
+
21
+ class AdminProfile < Profile
22
+ active_store_accessor :info, level: :integer
23
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_store_accessor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Pchelincev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
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.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
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
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4.7'
69
+ description: Get more from ActiveRecord::Store
70
+ email:
71
+ - mail@sergeyp.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - active_store_accessor.gemspec
83
+ - lib/active_store_accessor.rb
84
+ - tests/lib/active_store_accessor_test.rb
85
+ - tests/test_helper.rb
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Get more from ActiveRecord::Store
110
+ test_files: []