lightweight_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
+ SHA256:
3
+ metadata.gz: 7df7c6ebe17e4c439b7ed38a441aa8ac44c4b9d1975a76d86512de4dbaf4c26c
4
+ data.tar.gz: 426c0faf192370553bf79957b245bbd9215076f2a2c244314fba8fa8de45487a
5
+ SHA512:
6
+ metadata.gz: 07163dfc2a2b2fed8f3d456f1aabd854c34a9d4466ad8965cd362cb257072c02104e1767d0497e0c25207b1a63314d0c4a13d3abd8a9955fe6290b642c4985ae
7
+ data.tar.gz: 3892261d922943e24444a6b8889ec9cd644c325dd74332d013f848450bf081e741aff6f0218be372df4907b9cec05de9f7885e95e4daec4b83fc7f078920a89c
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.6.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in lightweight_attributes.gemspec
6
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Akira Matsuda
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,39 @@
1
+ # LightweightAttributes
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lightweight_attributes`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'lightweight_attributes'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install lightweight_attributes
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/amatsuda/lightweight_attributes.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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 << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lightweight_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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LightweightAttributes
4
+ class AttributeSet
5
+ class Builder
6
+ attr_reader :types, :default_attributes
7
+
8
+ def initialize(types, default_attributes = {})
9
+ @types = types
10
+ @default_attributes = default_attributes
11
+ end
12
+
13
+ def build_from_database(values = {}, _additional_types = {})
14
+ LightweightAttributes::AttributeSet.new values
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'attribute_set/builder'
4
+
5
+ module LightweightAttributes
6
+ class AttributeSet
7
+ delegate :each_value, :fetch, :except, :[], :[]=, :key?, :keys, to: :attributes
8
+
9
+ def initialize(attributes)
10
+ @attributes = attributes
11
+ end
12
+
13
+ def fetch_value(name)
14
+ self[name]
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :attributes
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_methods'
4
+
5
+ module LightweightAttributes
6
+ module BaseClassMethods
7
+ def _default_attributes # :nodoc:
8
+ load_schema
9
+ @default_attributes ||= if attributes_to_define_after_schema_loads.empty?
10
+ LightweightAttributes::AttributeSet.new({})
11
+ else
12
+ ActiveModel::AttributeSet.new({})
13
+ end
14
+ end
15
+
16
+ def attributes_builder
17
+ if attributes_to_define_after_schema_loads.empty?
18
+ unless defined?(@attributes_builder) && @attributes_builder
19
+ defaults = _default_attributes.except(*(column_names - [primary_key]))
20
+ @attributes_builder = LightweightAttributes::AttributeSet::Builder.new(attribute_types, defaults)
21
+ end
22
+ @attributes_builder
23
+ else
24
+ super
25
+ end
26
+ end
27
+
28
+ def load_schema!
29
+ super
30
+ include BaseMethods if attributes_to_define_after_schema_loads.empty?
31
+ end
32
+
33
+ #TODO: maybe we need to properly handle other non-nil values
34
+ private def define_default_attribute(name, value, type, from_user:)
35
+ super if attributes_to_define_after_schema_loads.any?
36
+
37
+ if value.nil? || (value == ActiveRecord::Attributes::ClassMethods.const_get(:NO_DEFAULT_PROVIDED))
38
+ _default_attributes[name] = nil
39
+ else
40
+ super
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LightweightAttributes
4
+ module BaseMethods
5
+ def attributes_before_type_cast
6
+ @attributes
7
+ end
8
+
9
+ def read_attribute_before_type_cast(attr_name)
10
+ @attributes[attr_name]
11
+ end
12
+
13
+ def _write_attribute(attr_name, value)
14
+ was = @attributes[attr_name]
15
+ changed_attributes[attr_name] = was unless changed_attributes.key? attr_name
16
+ @attributes[attr_name] = value
17
+ end
18
+
19
+ #NOTE: Should be true for user posted Time Hash, but who cares?
20
+ def attribute_came_from_user?(_attribute_name)
21
+ false
22
+ end
23
+
24
+ def will_save_change_to_attribute?(attr_name, **options)
25
+ changed_attributes.key? attr_name
26
+ end
27
+
28
+ def attribute_in_database(attr_name)
29
+ changed_attributes.key?(attr_name) ? changed_attributes[attr_name] : @attributes[attr_name]
30
+ end
31
+
32
+ def changes_to_save
33
+ changed_attributes.each_with_object({}) {|(k, v), h| h[k] = [v, @attributes[k]]}
34
+ end
35
+
36
+ def changed_attribute_names_to_save
37
+ changed_attributes.keys
38
+ end
39
+
40
+ def has_changes_to_save?
41
+ changed_attributes.any?
42
+ end
43
+
44
+ def changed_attributes
45
+ @changed_attributes ||= {}
46
+ end
47
+
48
+ def changed?(attr_name, **_options)
49
+ changed_attributes.key? attr_name
50
+ end
51
+
52
+ private def forget_attribute_assignments
53
+ @changed_attributes.clear
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LightweightAttributes
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'lightweight_attributes' do
6
+ ActiveSupport.on_load :active_record do
7
+ extend LightweightAttributes::BaseClassMethods
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lightweight_attributes/version"
4
+ require_relative 'lightweight_attributes/railtie'
5
+ require_relative 'lightweight_attributes/base_class_methods'
6
+ require_relative 'lightweight_attributes/attribute_set'
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "lightweight_attributes"
6
+ spec.version = '0.1.0'.freeze
7
+ spec.authors = ["Akira Matsuda"]
8
+ spec.email = ["ronnie@dio.jp"]
9
+
10
+ spec.summary = 'Good old lightweight attributes for Active Record'
11
+ spec.description = 'Bring the speed back to your Active Record models!'
12
+ spec.homepage = 'https://github.com/amatsuda/lightweight_attributes'
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightweight_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akira Matsuda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-12 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Bring the speed back to your Active Record models!
56
+ email:
57
+ - ronnie@dio.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - MIT-LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/lightweight_attributes.rb
71
+ - lib/lightweight_attributes/attribute_set.rb
72
+ - lib/lightweight_attributes/attribute_set/builder.rb
73
+ - lib/lightweight_attributes/base_class_methods.rb
74
+ - lib/lightweight_attributes/base_methods.rb
75
+ - lib/lightweight_attributes/railtie.rb
76
+ - lightweight_attributes.gemspec
77
+ homepage: https://github.com/amatsuda/lightweight_attributes
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.7.4
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Good old lightweight attributes for Active Record
101
+ test_files: []