attribute-depends-calculator 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a046d88fe7d1ad88d0ae1b29dd7041ccfc43b2d2
4
+ data.tar.gz: 6596be90ac350adf829691445a560a4cee535513
5
+ SHA512:
6
+ metadata.gz: 79677cba067d8d844f4acc3659f5688b12a932e5305aab398c23d306f0a88bcff11f98a42d5ea4905a316d22cc910d01633a5edfd9398e0b189b22c63808e3cd
7
+ data.tar.gz: be9f7bc1aa6256fdfed2ac55b29ac8d5d11dc67e8d9939ca7e0df2dd74870350cc621fae59babd9ab64612bea9d7bb6ce492a21a5a18b5dd08cc86e9e0bf6ab9
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-version
11
+ .idea/
12
+ .gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0
4
+ - 2.1.5
5
+ - 2.2
6
+ - 2.3.0
7
+ script: xvfb-run rspec
8
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attribute-depends-calculator.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rails', '>=4.2.5'
8
+ gem 'sqlite3', '>=1.3.6'
9
+ gem 'rspec', '~> 2.11'
10
+ gem 'database_cleaner'
11
+ gem 'coveralls', require: false
12
+ end
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Attribute Depends Calculator [![Build Status](https://travis-ci.org/falm/attribute-depends-calculator.svg?branch=master)](https://travis-ci.org/falm/attribute-depends-calculator) [![Coverage Status](https://coveralls.io/repos/github/falm/attribute-depends-calculator/badge.svg)](https://coveralls.io/github/falm/attribute-depends-calculator)
2
+
3
+ The scenario of the gem is when you have a attribute on model that value depends of a calculation of other model's attribute which attribute's model related. AttributeDependsCalculator will help you solve the case
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'attribute-depends-calculator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+ Assume you have model order and order-item
19
+ ```ruby
20
+ class Order < ActiveRecord::Base
21
+ has_many :order_items
22
+ depends total_price: {order_items: :price}
23
+ end
24
+
25
+ class OrderItem < ActiveRecord::Base
26
+ belongs_to :order
27
+ end
28
+ ```
29
+ And you can
30
+ ```ruby
31
+ order = Order.first
32
+ order.total_price
33
+ #=> 100.0
34
+ order.order_items.pluck(:price)
35
+ #=> [50.0, 50.0]
36
+ order_item = order.order_items.first
37
+ order_item.update(price: 100)
38
+ order.reload.total_price
39
+ #=> 150.0
40
+ ```
41
+ As above price of order automatically update when whatever order_items changes
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( http://github.com/zmbacker/enum_help/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
50
+
51
+ ## License
52
+ MIT © [Falm](https://github.com/falm)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attribute_depends_calculator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attribute-depends-calculator"
8
+ spec.version = AttributeDependsCalculator::VERSION
9
+ spec.authors = ["Jason Hou"]
10
+ spec.email = ["hjj1992@gmail.com"]
11
+
12
+ spec.summary = %q{calculate depends attribute automatically}
13
+ spec.description = %q{auto calculate collect of depends attribute value and save when that changes}
14
+ spec.homepage = "https://github.com/falm/attribute-depends-calculator"
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.licenses = ["MIT"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'rack', '~> 1.5'
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "attribute/depends/calculator"
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,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 @@
1
+ require 'attribute_depends_calculator'
@@ -0,0 +1,6 @@
1
+ #encoding: utf-8
2
+
3
+ Gem.find_files('attribute_depends_calculator/**/*.rb').each { |file| require file }
4
+
5
+ module AttributeDependsCalculator
6
+ end
@@ -0,0 +1,10 @@
1
+
2
+ module AttributeDependsCalculator
3
+
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace AttributeDependsCalculator
6
+ initializer 'attribute_depends_calculator.extend_active_record' do
7
+ ActiveRecord::Base.send :extend, AttributeDependsCalculator::Macro
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,67 @@
1
+ module AttributeDependsCalculator
2
+ class Factory
3
+ attr_accessor :klass, :column, :params, :association, :associate_column, :association_name
4
+ def initialize(klass, column, params)
5
+ self.klass, self.column = klass, column
6
+ self.association, self.associate_column, self.association_name = fetch_association(params)
7
+ end
8
+
9
+ def perform
10
+ define_calculator
11
+ append_callbacks
12
+ end
13
+
14
+ private
15
+
16
+ def define_calculator
17
+ self.klass.class_eval <<-METHOD, __FILE__, __LINE__
18
+ def #{calculate_method_name}
19
+ total = self.#{association_name}.pluck(:#{associate_column}).compact.reduce(0, :+)
20
+ update(:#{column} => total)
21
+ end
22
+ METHOD
23
+ end
24
+
25
+ def calculate_method_name
26
+ "calculate_and_update_#{column}"
27
+ end
28
+
29
+ def klass_assoc_name
30
+ klass.table_name.singularize
31
+ end
32
+
33
+ def fetch_association(params)
34
+ assoc = params.keys.first
35
+ [klass.reflect_on_association(assoc).class_name.constantize, params.values.first, assoc]
36
+ end
37
+
38
+ def append_callbacks
39
+ append_callback_hook
40
+ define_callback_methods
41
+ end
42
+
43
+ def append_callback_hook
44
+ self.association.class_eval <<-METHOD, __FILE__, __LINE__
45
+ after_save :depends_update_#{column}
46
+ around_destroy :depends_update_#{column}_around
47
+ METHOD
48
+ end
49
+
50
+ def define_callback_methods
51
+ self.association.class_eval <<-METHOD, __FILE__, __LINE__
52
+ private
53
+
54
+ def depends_update_#{column}
55
+ self.#{klass_assoc_name}.#{calculate_method_name}
56
+ end
57
+
58
+ def depends_update_#{column}_around
59
+ _relation = self.#{klass_assoc_name}
60
+ yield
61
+ _relation.#{calculate_method_name}
62
+ end
63
+ METHOD
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,11 @@
1
+ module AttributeDependsCalculator
2
+ module Macro
3
+ def depend(options)
4
+ options.each do |column, option|
5
+ Factory.new(self, column, option).perform
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ ActiveRecord::Base.send :extend, AttributeDependsCalculator::Macro
@@ -0,0 +1,4 @@
1
+
2
+ module AttributeDependsCalculator
3
+ VERSION = '0.1'
4
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute-depends-calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Jason Hou
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-23 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ description: auto calculate collect of depends attribute value and save when that
56
+ changes
57
+ email:
58
+ - hjj1992@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - README.md
68
+ - Rakefile
69
+ - attribute-depends-calculator.gemspec
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/attribute-depends-calculator.rb
73
+ - lib/attribute_depends_calculator.rb
74
+ - lib/attribute_depends_calculator/engine.rb
75
+ - lib/attribute_depends_calculator/factory.rb
76
+ - lib/attribute_depends_calculator/macro.rb
77
+ - lib/attribute_depends_calculator/version.rb
78
+ homepage: https://github.com/falm/attribute-depends-calculator
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: calculate depends attribute automatically
102
+ test_files: []