active_any 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rubocop.yml +27 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +6 -0
  7. data/Guardfile +10 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +92 -0
  10. data/Rakefile +12 -0
  11. data/active_any.gemspec +36 -0
  12. data/bin/console +12 -0
  13. data/bin/setup +8 -0
  14. data/lib/active_any/adapter.rb +13 -0
  15. data/lib/active_any/adapters/abstract_adapter.rb +27 -0
  16. data/lib/active_any/adapters/object_adapter.rb +74 -0
  17. data/lib/active_any/association_relation.rb +18 -0
  18. data/lib/active_any/associations/association.rb +94 -0
  19. data/lib/active_any/associations/association_scope.rb +35 -0
  20. data/lib/active_any/associations/belongs_to_association.rb +21 -0
  21. data/lib/active_any/associations/builder/association.rb +62 -0
  22. data/lib/active_any/associations/builder/belongs_to.rb +13 -0
  23. data/lib/active_any/associations/builder/has_many.rb +17 -0
  24. data/lib/active_any/associations/collection_proxy.rb +66 -0
  25. data/lib/active_any/associations/has_many_association.rb +43 -0
  26. data/lib/active_any/associations.rb +72 -0
  27. data/lib/active_any/attribute_assignment.rb +16 -0
  28. data/lib/active_any/core.rb +26 -0
  29. data/lib/active_any/finders.rb +28 -0
  30. data/lib/active_any/reflection/association_reflection.rb +88 -0
  31. data/lib/active_any/reflection/belongs_to_reflection.rb +29 -0
  32. data/lib/active_any/reflection/has_many_reflection.rb +29 -0
  33. data/lib/active_any/reflection.rb +49 -0
  34. data/lib/active_any/relation/merger.rb +72 -0
  35. data/lib/active_any/relation/order_clause.rb +60 -0
  36. data/lib/active_any/relation/where_clause.rb +73 -0
  37. data/lib/active_any/relation.rb +243 -0
  38. data/lib/active_any/version.rb +5 -0
  39. data/lib/active_any.rb +62 -0
  40. metadata +251 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fe7ee5723d9362864d198ce08fea398a304bed1e
4
+ data.tar.gz: f3bd88f1d456569eeed90e9471336a0c63f65ceb
5
+ SHA512:
6
+ metadata.gz: c429d20676d4b11e0ff92d22ffbea558198b148ed871c6ab2be531bb374b00b96f214c88ee54e5a812d836823aec646987a1ec847a3409a085187279658f15ca
7
+ data.tar.gz: 2ef9773b72a9063711f83615dedf345457f08146f839afefd160bdbbedb2d741b85784051c7d462527e04038e5170bbb2848cdf5b882d52203faeeb99e16e50c
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/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ Exclude:
4
+ - bin/console
5
+ - active_any.gemspec
6
+ - Guardfile
7
+ - vendor/bundle/**/*
8
+ DisplayCopNames: true
9
+
10
+ Style/Documentation:
11
+ Enabled: false
12
+
13
+ Style/PredicateName:
14
+ Enabled: false
15
+
16
+ Metrics/LineLength:
17
+ Max: 150
18
+ Exclude:
19
+ - test/**/*
20
+
21
+ Style/GuardClause:
22
+ Enabled: false
23
+
24
+ Metrics/AbcSize:
25
+ Max: 18
26
+ Exclude:
27
+ - test/**/*
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at yuemori@aiming-inc.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in active_any.gemspec
6
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard :minitest do
2
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
3
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ end
6
+
7
+ guard :rubocop do
8
+ watch(%r{.+\.rb$})
9
+ watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 yuemori
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,92 @@
1
+ # ActiveAny
2
+
3
+ A utility for quering interface to any objects like ActiveRecord. This gem support for querying, association and relation.
4
+
5
+ This gem is not stable version. Be careful.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'active_any'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install active_any
22
+
23
+ ## Usage
24
+
25
+ ### Getting Started
26
+
27
+ ```ruby
28
+ class User < ActiveAny::Object
29
+ attr_accessor :id, :name, :age
30
+
31
+ self.data = [
32
+ new(id: 1, name: 'alice', age: 20),
33
+ new(id: 2, name: 'bob', age: 20),
34
+ new(id: 3, name: 'charlie', age: 20),
35
+ new(id: 4, name: 'alice', age: 30),
36
+ new(id: 5, name: 'bob', age: 30),
37
+ new(id: 6, name: 'charlie', age: 30),
38
+ new(id: 7, name: 'alice', age: 40),
39
+ new(id: 8, name: 'bob', age: 40),
40
+ new(id: 9, name: 'charlie', age: 40),
41
+ new(id: 10, name: 'alice', age: 50),
42
+ new(id: 11, name: 'bob', age: 50),
43
+ new(id: 12, name: 'charlie', age: 50)
44
+ ]
45
+ end
46
+
47
+ User.all #=> Relation
48
+ User.all.to_a #=> Array of User
49
+ User.where(name: 'alice') #=> WHERE name equals alice
50
+ User.where(name: /li/) #=> WHERE name LIKE li (alice, charlie)
51
+ User.group(:name) #=> GROUP BY name
52
+ User.order(:age) #=> ORDER BY age
53
+ User.order(age: :desc) #=> ORDER BY age DESC
54
+ User.limit(5) #=> LIMIT 5
55
+
56
+ User.where(name: 'alice').group(:age).order(age: :desc).limit(1).first # Chain
57
+ ```
58
+
59
+ Clone repository and run `bin/console`, if you want to try it!
60
+ (See [test/data.rb](test/data.rb) for defined data on console.)
61
+
62
+ ## Features
63
+
64
+ - more powerful interface for select query
65
+ - [] `joins` (interface only)
66
+ - [] `includes`
67
+ - [] `having`
68
+ - [] Interface for other than select query (create, destroy, update)
69
+ - [] enum support
70
+ - [] logging
71
+ - [] scoping
72
+ - [] ActiveRecord integration
73
+
74
+ ## Testing
75
+
76
+ ```sh
77
+ bin/setup
78
+ bundle exec rake test
79
+ bundle exec rubocop
80
+ ```
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yuemori/active_any. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
89
+
90
+ ## Code of Conduct
91
+
92
+ Everyone interacting in the ActiveAny project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/active_any/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_any/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_any"
8
+ spec.version = ActiveAny::VERSION
9
+ spec.authors = ["yuemori"]
10
+ spec.email = ["yuemori@aiming-inc.com"]
11
+
12
+ spec.summary = %q{A utility for quering interface to any objects like ActiveRecord.}
13
+ spec.description = %q{A utility for quering interface to any objects like ActiveRecord. This gem support for querying, association, enum, and relation.}
14
+ spec.homepage = "https://github.com/yuemori/active_any"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "activesupport"
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency "minitest-power_assert"
29
+ spec.add_development_dependency "guard-minitest"
30
+ spec.add_development_dependency "guard-rubocop"
31
+ spec.add_development_dependency "rubocop"
32
+ spec.add_development_dependency "pry-byebug"
33
+ spec.add_development_dependency "pry-coolline"
34
+ spec.add_development_dependency "pry-inline"
35
+ spec.add_development_dependency "pry-state"
36
+ end
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'active_any'
6
+ require 'pry'
7
+ require 'pry-coolline'
8
+ require_relative '../test/data'
9
+
10
+ include TestData
11
+
12
+ Pry.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,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAny
4
+ class Adapter
5
+ def initialize(klass)
6
+ @klass = klass
7
+ end
8
+
9
+ def query(_where_clause, _limit_value)
10
+ raise NotImplementedError
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAny
4
+ class AbstractAdapter < Adapter
5
+ %i[base_hander regexp_handler range_handler array_handler].each do |method|
6
+ define_method method do |_record, _key, value|
7
+ raise NotImplementedError.new, "#{self.class.name} can not handle for #{value.class}"
8
+ end
9
+ end
10
+
11
+ def query(_where_clause, _limit_value = nil, _group_values = [], _order_values = [])
12
+ raise NotImplementedError.new, "#{self.class.name} can not handle for #{value.class}"
13
+ end
14
+
15
+ def limit_handler(_records, _limit_value)
16
+ raise NotImplementedError.new, "#{self.class.name} can not handle for limit"
17
+ end
18
+
19
+ def group_handler(_records, _group_values)
20
+ raise NotImplementedError.new, "#{self.class.name} can not handle for group"
21
+ end
22
+
23
+ def order_handler(_records, _order_values)
24
+ raise NotImplementedError.new, "#{self.class.name} can not handle for order"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAny
4
+ class ObjectAdapter < AbstractAdapter
5
+ def query(where_clause: Relation::WhereClause.empty, order_clause: Relation::OrderClause.empty, limit_value: nil, group_values: [])
6
+ records = select_handler(@klass.data, where_clause)
7
+ records = group_handler(records, group_values)
8
+ records = limit_handler(records, limit_value)
9
+ records = order_handler(records, order_clause)
10
+ records
11
+ end
12
+
13
+ def select_handler(records, where_clause)
14
+ records.select do |record|
15
+ where_clause.all? do |condition|
16
+ condition.evaluate(self, record)
17
+ end
18
+ end
19
+ end
20
+
21
+ def limit_handler(records, limit_value)
22
+ limit_value ? records.take(limit_value) : records
23
+ end
24
+
25
+ def order_handler(records, order_clause)
26
+ ordered_records = order_clause.empty? ? records : sort_by_order_clause(records, order_clause)
27
+ order_clause.reverse? ? ordered_records.reverse : ordered_records
28
+ end
29
+
30
+ def base_handler(record, key, value)
31
+ record.send(key) == value
32
+ end
33
+
34
+ def regexp_handler(record, key, regexp)
35
+ regexp.match? record.send(key)
36
+ end
37
+
38
+ def range_handler(record, key, range)
39
+ range.include? record.send(key)
40
+ end
41
+
42
+ def array_handler(record, key, array)
43
+ array.include? record.send(key)
44
+ end
45
+
46
+ def group_handler(records, group_values)
47
+ return records if group_values.empty?
48
+
49
+ records.uniq do |record|
50
+ group_values.map { |method| record.send(method) }
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def build_order_proc(a, b, order_value)
57
+ case order_value.sort_type
58
+ when :asc then proc { a.send(order_value.key) <=> b.send(order_value.key) }
59
+ when :desc then proc { -(a.send(order_value.key) <=> b.send(order_value.key)) }
60
+ else
61
+ raise ArgumentError, "#{order_value.sort_type} is not supported"
62
+ end
63
+ end
64
+
65
+ def sort_by_order_clause(records, order_clause)
66
+ records.sort do |a, b|
67
+ blocks = order_clause.order_values.map do |order_value|
68
+ build_order_proc(a, b, order_value)
69
+ end
70
+ blocks.map(&:call).find(&:nonzero?) || 0
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAny
4
+ class AssociationRelation < Relation
5
+ def initialize(klass, association)
6
+ super(klass)
7
+ @association = association
8
+ end
9
+
10
+ def proxy_association
11
+ @association
12
+ end
13
+
14
+ def ==(other)
15
+ other == records
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAny
4
+ module Associations
5
+ class Association
6
+ attr_reader :loaded, :reflection, :target, :owner
7
+
8
+ def initialize(owner, reflection)
9
+ @owner = owner
10
+ @reflection = reflection
11
+
12
+ reset
13
+ reset_scope
14
+ end
15
+
16
+ def reader
17
+ # TODO: implement
18
+ # reload if stale_target?
19
+
20
+ @proxy ||= CollectionProxy.create(owner, self)
21
+ @proxy.reset_scope
22
+ end
23
+
24
+ def writer(_records)
25
+ raise NotImplementedError.new, 'writer is unimplemented'
26
+ end
27
+
28
+ def scope
29
+ target_scope.merge!(association_scope)
30
+ end
31
+
32
+ def target_scope
33
+ AssociationRelation.create(klass, self).merge!(klass.all)
34
+ end
35
+
36
+ def association_scope
37
+ @association_scope ||= klass ? AssociationScope.scope(self) : nil
38
+ end
39
+
40
+ def reload
41
+ reset
42
+ reset_scope
43
+ load_target
44
+ self unless target.nil?
45
+ end
46
+
47
+ def klass
48
+ reflection.klass
49
+ end
50
+
51
+ def load_target
52
+ @target = find_target if find_target?
53
+
54
+ loaded! unless loaded?
55
+ target
56
+ # TODO: implement
57
+ # rescue ActiveRecord::RecordNotFound
58
+ # reset
59
+ end
60
+
61
+ def reset
62
+ @loaded = false
63
+ @stale_target = nil
64
+ end
65
+
66
+ def reset_scope
67
+ @association_scope = nil
68
+ end
69
+
70
+ def loaded?
71
+ @loaded
72
+ end
73
+
74
+ def find_target
75
+ raise NotImplementedError
76
+ end
77
+
78
+ def loaded!
79
+ @loaded = true
80
+ end
81
+
82
+ def target=(target)
83
+ @target = target
84
+ loaded!
85
+ end
86
+
87
+ private
88
+
89
+ def find_target?
90
+ !loaded? && klass
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAny
4
+ module Associations
5
+ class AssociationScope
6
+ def self.scope(association)
7
+ INSTANCE.scope(association)
8
+ end
9
+
10
+ def self.create
11
+ new
12
+ end
13
+
14
+ INSTANCE = create
15
+
16
+ def scope(association)
17
+ klass = association.klass
18
+ reflection = association.reflection
19
+ scope = klass.all
20
+ owner = association.owner
21
+
22
+ add_constraints(scope, owner, reflection)
23
+ end
24
+
25
+ private
26
+
27
+ def add_constraints(scope, owner, reflection)
28
+ join_keys = reflection.join_keys
29
+ key = join_keys.key
30
+ value = owner.send(join_keys.foreign_key)
31
+ scope.where(key => value)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAny
4
+ module Associations
5
+ class BelongsToAssociation < Association
6
+ def reader
7
+ reload unless loaded?
8
+
9
+ target
10
+ end
11
+
12
+ def writer(_records)
13
+ raise NotImplementedError.new, 'writer is unimplemented'
14
+ end
15
+
16
+ def find_target
17
+ scope.first
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAny
4
+ module Associations
5
+ module Builder
6
+ class Association
7
+ def self.build(klass, name, scope, options)
8
+ reflection = create_reflection(klass, name, scope, options)
9
+ define_writer_method klass, reflection.name
10
+ define_reader_method klass, reflection.name
11
+ reflection
12
+ end
13
+
14
+ VALID_OPTIONS = %i[class_name foreign_key].freeze
15
+
16
+ def self.valid_options
17
+ VALID_OPTIONS
18
+ end
19
+
20
+ def self.validate_options(options)
21
+ options.assert_valid_keys(valid_options)
22
+ end
23
+
24
+ def self.create_reflection(klass, name, scope, options)
25
+ if scope.is_a?(Hash)
26
+ options = scope
27
+ scope = nil
28
+ end
29
+
30
+ validate_options(options)
31
+ scope = build_scope scope
32
+
33
+ ActiveAny::Reflection.create(macro, name, scope, options, klass)
34
+ end
35
+
36
+ def self.build_scope(scope)
37
+ new_scope = scope
38
+
39
+ new_scope = proc { instance_exec(&scope) } if scope && scope.arity.zero?
40
+
41
+ new_scope
42
+ end
43
+
44
+ def self.define_reader_method(klass, name)
45
+ klass.class_eval <<-CODE, __FILE__, __LINE__ + 1
46
+ def #{name}(*args)
47
+ association(:#{name}).reader(*args)
48
+ end
49
+ CODE
50
+ end
51
+
52
+ def self.define_writer_method(klass, name)
53
+ klass.class_eval <<-CODE, __FILE__, __LINE__ + 1
54
+ def #{name}=(value)
55
+ association(:#{name}).writer(value)
56
+ end
57
+ CODE
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end