lazy_attributes 0.0.1

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: e8bf395ec4389966e4dad936c0fa8094fd39c625
4
+ data.tar.gz: 13fe587729ebaaaec642692eb01b524635eb1d56
5
+ SHA512:
6
+ metadata.gz: ee918019f8d72321362217300e9a1c62a62f05f2bc4e90a406ab029743a2cdbb58c235ba0d1893881067116baab1691e3b94f55d955dbc8cce2efc3d8108481a
7
+ data.tar.gz: f70019113a657950c15dd9090de4297cca2b326aa743084fedab4af302236e54afc8f972de4f15988a9d4d249cb4e32346741b5debf60e6cd259662094ebfab2
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lazy_attributes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Eito Katagiri
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,29 @@
1
+ # LazyAttributes
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'lazy_attributes'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lazy_attributes
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/lazy_attributes/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ require 'bundler/setup'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+
7
+ task default: :spec
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
@@ -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 'lazy_attributes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lazy_attributes"
8
+ spec.version = LazyAttributes::VERSION
9
+ spec.authors = ["Eito Katagiri"]
10
+ spec.email = ["eitoball@gmail.com"]
11
+ spec.summary = %q{ActiveRecord plugin to load specified attributes lazyly}
12
+ spec.description = %q{ActiveRecord plugin to load specified attributes lazyly}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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 'activerecord', '>= 3.2', '< 5'
22
+
23
+ spec.add_development_dependency 'database_cleaner'
24
+ spec.add_development_dependency 'bundler', '~> 1.6'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'sqlite3'
28
+ end
@@ -0,0 +1,3 @@
1
+ module LazyAttributes
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,56 @@
1
+ require 'lazy_attributes/version'
2
+
3
+ module LazyAttributes
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :_lazy_attributes, instance_writer: false
8
+ self._lazy_attributes = []
9
+ end
10
+
11
+ def reload_but_keep_changes
12
+ return unless persisted?
13
+ changes_before_reload = changes.clone
14
+ reload
15
+ changes_before_reload.each do |attr_name, values|
16
+ send("#{attr_name}=", values[1])
17
+ end
18
+ end
19
+
20
+ module ClassMethods
21
+ def lazy_attributes(*attrs)
22
+ if self._lazy_attributes.empty?
23
+ default_scope do
24
+ select(column_names_without_lazy.map do |column_name|
25
+ "#{table_name}.#{column_name}"
26
+ end)
27
+ end
28
+ end
29
+ attrs = attrs.map(&:to_s)
30
+ attrs.each do |attr|
31
+ attr = attr.to_sym
32
+ define_method(attr) do
33
+ reload_but_keep_changes unless has_attribute?(attr)
34
+ read_attribute(attr)
35
+ end
36
+ define_method(:"#{attr}=") do |val|
37
+ reload_but_keep_changes unless has_attribute?(attr)
38
+ write_attribute(attr, val)
39
+ end
40
+ end
41
+ self._lazy_attributes += attrs
42
+ end
43
+
44
+ def column_names_without_lazy
45
+ column_names - self._lazy_attributes
46
+ end
47
+
48
+ def column_symbols_without_lazy
49
+ column_names_without_lazy.map(&:to_sym)
50
+ end
51
+ end
52
+ end
53
+
54
+ ActiveSupport.on_load :active_record do
55
+ ActiveRecord::Base.send(:include, LazyAttributes)
56
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe LazyAttributes do
6
+ it '' do
7
+ expect do
8
+ User.send(:lazy_attributes, :profile)
9
+ User.send(:lazy_attributes, :address)
10
+ end.to_not raise_error
11
+
12
+ User.create!(
13
+ name: 'John',
14
+ profile: 'This attribute is suppose to be very long',
15
+ address: 'Somewhere over the rainbow'
16
+ )
17
+
18
+ user = User.first
19
+ expect(user).to_not have_attribute(:profile)
20
+ expect(user).to_not have_attribute(:address)
21
+ expect(user.profile).to be_present
22
+ expect(user.address).to eq('Somewhere over the rainbow')
23
+ end
24
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w{ .. lib }))
4
+ require 'rspec'
5
+ require 'active_record'
6
+ require 'lazy_attributes'
7
+ require 'database_cleaner'
8
+
9
+ ActiveRecord::Base.establish_connection(
10
+ adapter: 'sqlite3',
11
+ database: ':memory:'
12
+ )
13
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
14
+
15
+ class User < ActiveRecord::Base
16
+ end
17
+
18
+ class CreateTables < ActiveRecord::Migration
19
+ def up
20
+ create_table :users do |t|
21
+ t.string :name
22
+ t.text :profile
23
+ t.string :address
24
+ end
25
+ end
26
+ end
27
+
28
+ RSpec.configure do |config|
29
+ config.before(:suite) do
30
+ DatabaseCleaner.strategy = :truncation
31
+ DatabaseCleaner.clean_with(:truncation)
32
+ end
33
+
34
+ config.before(:all) do
35
+ CreateTables.new.up unless ActiveRecord::Base.connection.table_exists?('users')
36
+ end
37
+
38
+ config.after(:each) do
39
+ User.delete_all
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eito Katagiri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-06 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
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: database_cleaner
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.6'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.6'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: sqlite3
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description: ActiveRecord plugin to load specified attributes lazyly
104
+ email:
105
+ - eitoball@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - lazy_attributes.gemspec
116
+ - lib/lazy_attributes.rb
117
+ - lib/lazy_attributes/version.rb
118
+ - spec/lazy_attributes_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: ''
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: ActiveRecord plugin to load specified attributes lazyly
144
+ test_files:
145
+ - spec/lazy_attributes_spec.rb
146
+ - spec/spec_helper.rb