serialized_virtual_attributes 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: 18067bd9816c2051700b797175601460e56ff152
4
+ data.tar.gz: e1a0fb1b60d17a94517c349aca9bc53890f7951d
5
+ SHA512:
6
+ metadata.gz: 04497741cbdb94afd23c6eae9b7fed7ad84734772ace40fdbec289c83967421e4b9037fa96082f9f4e3f81616a4788734085658625f7621fcd5ea6488f361063
7
+ data.tar.gz: 359f9003f757ef2afd6153ccbb99b39c5408f7a0abf78eb5e107ee99f6806b4ee517a0eddfcdccec8404bff10ec9754daba0240c2df4305adf019402303d6aa1
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /spec/db/*.sqlite
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in serialized_virtual_attributes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Sergey.Gnuskov
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,66 @@
1
+ # SerializedVirtualAttributes
2
+
3
+ This gem provides ability to have several attributes in one ActiveRecord serialized column. It might be useful for STI classes, that have some different attributes and you don't want to have separate columns for each class.
4
+
5
+ ## Requirements
6
+
7
+ This gem requires ActiveRecord 3.0 or higher and ruby 1.9 or higher.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'serialized_virtual_attributes'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install serialized_virtual_attributes
24
+
25
+ ## Usage
26
+
27
+ Just create text column, serialize it to `Hash` and define your virtual attributes using `serialized_virtual_attribute`:
28
+ ```ruby
29
+ class Server < ActiveRecord::Base
30
+ serialize :configuration, Hash
31
+
32
+ # basic attributes
33
+ serialized_virtual_attribute :host, :description, to: :configuration
34
+
35
+ # attribute with typecast
36
+ serialized_virtual_attribute :port, to: :configuration, typecast: Integer
37
+
38
+ # attributes with prefixed accessors
39
+ serialized_virtual_attribute :name, to: :configuration, prefix: :network
40
+ end
41
+ ```
42
+ Then you are able to use accessors:
43
+
44
+ ```ruby
45
+ serv = Server.new
46
+ serv.host = 'localhost'
47
+ serv.port = 9232
48
+ serv.network_name = 'oxygen'
49
+ ```
50
+
51
+ For ActiveRecord < 4 attr_accessible is enabled by default. You can disable it by providing `accessible: false`.
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/serialized_virtual_attributes.
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "serialized_virtual_attributes"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,70 @@
1
+ require "serialized_virtual_attributes/version"
2
+ require 'active_support'
3
+
4
+ module SerializedVirtualAttributes
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :serialized_virtual_attributes
9
+ end
10
+
11
+ module ClassMethods
12
+ def serialized_virtual_attribute(*args)
13
+ options = args.extract_options!
14
+ to = options[:to]
15
+ raise ArgumentError.new("option 'to' not provided or it is not serialized to hash") if to.blank? or self.serialized_attributes[to.to_s].try(:object_class) != Hash
16
+
17
+ typecast = options[:typecast]
18
+
19
+ self.serialized_virtual_attributes ||= {}
20
+ self.serialized_virtual_attributes[to] ||= Set.new
21
+
22
+ prefix = options[:prefix]
23
+
24
+ args.each do |a|
25
+ accessor_name = prefix.blank? ? a : :"#{prefix}_#{a}"
26
+ self.serialized_virtual_attributes[to] += [accessor_name]
27
+ class_eval "
28
+ def #{accessor_name}
29
+ self.#{to} = {} if self.#{to}.blank?
30
+
31
+ self.#{to}[:#{a}]
32
+ end
33
+ "
34
+
35
+ if typecast.blank?
36
+ class_eval "
37
+ def #{accessor_name}=(value)
38
+ self.#{to} = {} if self.#{to}.blank?
39
+
40
+ #{to}_will_change! unless value == self.#{to}[:#{a}]
41
+
42
+ self.#{to}[:#{a}] = value
43
+ end
44
+ "
45
+ else
46
+ class_eval "
47
+ def #{accessor_name}=(value)
48
+ self.#{to} = {} if self.#{to}.blank?
49
+
50
+ value = #{typecast.name}(value) rescue nil
51
+ #{to}_will_change! unless value == self.#{to}[:#{a}]
52
+
53
+ self.#{to}[:#{a}] = value
54
+ end
55
+ "
56
+ end
57
+
58
+ unless ActiveRecord::VERSION::MAJOR >= 4 or options[:accessible] == false
59
+ class_eval "
60
+ attr_accessible :#{accessor_name}
61
+ "
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ ActiveSupport.on_load :active_record do
69
+ ActiveRecord::Base.send :include, ::SerializedVirtualAttributes
70
+ end
@@ -0,0 +1,3 @@
1
+ module SerializedVirtualAttributes
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'serialized_virtual_attributes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "serialized_virtual_attributes"
8
+ spec.version = SerializedVirtualAttributes::VERSION
9
+ spec.authors = ["Sergey.Gnuskov"]
10
+ spec.email = ["sergey.gnuskov@flant.ru"]
11
+
12
+ spec.summary = %q{Ability to have several activerecord-like attributes in a single serialized column}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = '>= 1.9'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "sqlite3"
26
+
27
+ spec.add_runtime_dependency "activerecord", ">= 3.2"
28
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serialized_virtual_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey.Gnuskov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ description:
84
+ email:
85
+ - sergey.gnuskov@flant.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/serialized_virtual_attributes.rb
100
+ - lib/serialized_virtual_attributes/version.rb
101
+ - serialized_virtual_attributes.gemspec
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '1.9'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.3
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Ability to have several activerecord-like attributes in a single serialized
126
+ column
127
+ test_files: []