json_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: 21b7be213ed1d90c4eaf28eab315b610b145b2cf
4
+ data.tar.gz: df9f9ef3066c7bc2278cebca14f55519e1ef560d
5
+ SHA512:
6
+ metadata.gz: bf23c52264ec498ddeb7b689e3b3390317975dfdb4af26d4c1c0551045bc3faea435513d51b74a089472ee0a6987e682ff8c8515cd03054db3414075916e8d7a
7
+ data.tar.gz: 3fe220a993cd01a5371dc3bb42f6cf705df0e6b576d6817500e8af5cc52f59f413e5618186c8249feedfa725184c253715ecf2a7141f490ce5827d9b60582bd0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ json_attributes
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.2.2
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### VERSION 0.1.0
2
+
3
+ * First release
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_attributes.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ gem 'pry'
9
+ gem 'pg'
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Joel AZEMAR
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,96 @@
1
+ # JsonAttributes
2
+
3
+ [![Code Climate](https://codeclimate.com/github/joel/json_attributes.png)](https://codeclimate.com/github/joel/json_attributes)
4
+
5
+ [![Dependency Status](https://gemnasium.com/joel/json_attributes.svg)](https://gemnasium.com/joel/json_attributes)
6
+
7
+ [![Build Status](https://travis-ci.org/joel/json_attributes.svg?branch=master)](https://travis-ci.org/joel/json_attributes) (Travis CI)
8
+
9
+ [![Coverage Status](https://coveralls.io/repos/joel/json_attributes/badge.svg?branch=master&service=github)](https://coveralls.io/github/joel/json_attributes?branch=master)
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/json_attributes.svg)](http://badge.fury.io/rb/json_attributes)
12
+
13
+ This gem allow you to use json column of last version of postgres sql in transparenlty manner, this means you can access on your attributes into the json like a normal attribute
14
+
15
+ For instance
16
+
17
+ define a Model with json column like that
18
+
19
+ ```ruby
20
+ ActiveRecord::Schema.define do
21
+ create_table(:users) do |t|
22
+ t.string :name
23
+ t.json :office
24
+ end
25
+ end
26
+ ```
27
+
28
+ Define your model like that
29
+ ```ruby
30
+ class User < ActiveRecord::Base
31
+ include JsonAttributes
32
+ json_attributes :office, :address
33
+ end
34
+ ```
35
+
36
+ Create a instance
37
+ ```ruby
38
+ user = User.create(name: 'John Doe', office: { address: '1 Infinite Loop, Cupertino, CA 95014' })
39
+ ```
40
+
41
+ You can ask
42
+ ```ruby
43
+ user.name # => 'John Doe'
44
+ user.name? # => true
45
+ user.name = 'Jane Doe'
46
+
47
+ user.address # => '1 Infinite Loop, Cupertino, CA 95014'
48
+ user.address? # => true
49
+ user.address = '75 Ninth Avenue 2nd and 4th Floors New York, NY 10011.'
50
+ ```
51
+
52
+ You can use magic search methods as well
53
+ ```ruby
54
+ User.find_by_name('John Doe') # => <#User:3456...>
55
+ User.find_by_address('1 Infinite Loop, Cupertino, CA 95014') # => <#User:3456...>
56
+ ```
57
+
58
+ All attributes are provided through #attributes method
59
+ ```ruby
60
+ user.attributes #=> { 'id'=>1, 'name'=>'John Doe', 'address'=>'1 Infinite Loop, Cupertino, CA 95014' }
61
+ ```
62
+
63
+ ## Installation
64
+
65
+ Add this line to your application's Gemfile:
66
+
67
+ ```ruby
68
+ gem 'json_attributes'
69
+ ```
70
+
71
+ And then execute:
72
+
73
+ $ bundle
74
+
75
+ Or install it yourself as:
76
+
77
+ $ gem install json_attributes
78
+
79
+ ## Usage
80
+
81
+ TODO: Write usage instructions here
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
+
87
+ 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).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json_attributes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
92
+
93
+
94
+ ## License
95
+
96
+ 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/UPGRADE.md ADDED
@@ -0,0 +1 @@
1
+ # Upgrading
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json_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,25 @@
1
+ require_relative 'lib/json_attributes/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'json_attributes'
5
+ spec.version = JsonAttributes::VERSION
6
+ spec.authors = ['Joel AZEMAR']
7
+ spec.email = ['joel.azemar@gmail.com']
8
+
9
+ spec.summary = %q{Allow to use postgres json field in a totally transparently manner}
10
+ spec.description = %q{Allow to use postgres json field in a totally transparently manner with ActiveRecord}
11
+ spec.homepage = 'https://github.com/joel/json_attributes'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.bindir = 'exe'
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'activerecord', '~> 4.2'
20
+ spec.add_dependency 'activesupport', '~> 4.2'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec'
25
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support'
2
+
3
+ require 'json_attributes/version'
4
+
5
+ require 'json_attributes/legacy_predicates'
6
+ require 'json_attributes/legacy_attributes'
7
+
8
+ module JsonAttributes
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ include LegacyPredicates
13
+ prepend LegacyAttributes
14
+ end
15
+
16
+ class_methods do
17
+ def json_attributes(field, *json_attrs)
18
+ @json_attrs = json_attrs
19
+ @field = field
20
+
21
+ define_question_methods
22
+
23
+ store_accessor field, *json_attrs
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ module JsonAttributes::LegacyAttributes
2
+
3
+ def attributes
4
+ _json_attrs = (self.send(self.class.instance_variable_get(:@field)).try(:dup) || {}).with_indifferent_access
5
+
6
+ self.class.instance_variable_get(:@json_attrs).each do |_attribute|
7
+ _json_attrs[_attribute] = nil unless _json_attrs.has_key?(_attribute)
8
+ _json_attrs[_attribute] ||= self.send(_attribute)
9
+ end
10
+
11
+ super.merge(_json_attrs).with_indifferent_access.slice!(self.class.instance_variable_get(:@field))
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module JsonAttributes::LegacyPredicates
2
+ extend ActiveSupport::Concern
3
+
4
+ class_methods do
5
+ def define_question_methods
6
+ @json_attrs.each do |attribute|
7
+ define_method :"#{attribute}?" do
8
+ !!(self.send(self.class.instance_variable_get(:@field)) && self.send(self.class.instance_variable_get(:@field))[attribute.to_s])
9
+ end
10
+ define_singleton_method :"find_by_#{attribute}" do |value|
11
+ self.where("#{self.instance_variable_get(:@field)}->>'#{attribute}' = ?", value).first
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module JsonAttributes
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel AZEMAR
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-30 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: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Allow to use postgres json field in a totally transparently manner with
84
+ ActiveRecord
85
+ email:
86
+ - joel.azemar@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".ruby-gemset"
94
+ - ".ruby-version"
95
+ - ".travis.yml"
96
+ - CHANGELOG.md
97
+ - CODE_OF_CONDUCT.md
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - UPGRADE.md
103
+ - bin/console
104
+ - bin/setup
105
+ - json_attributes.gemspec
106
+ - lib/json_attributes.rb
107
+ - lib/json_attributes/legacy_attributes.rb
108
+ - lib/json_attributes/legacy_predicates.rb
109
+ - lib/json_attributes/version.rb
110
+ homepage: https://github.com/joel/json_attributes
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.4.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Allow to use postgres json field in a totally transparently manner
134
+ test_files: []