flatter-extensions 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: 836dcba21ba1e7372ffb7ba02c0c830caab448da
4
+ data.tar.gz: d6a27a477a4de896ab938cc9098b5e7e60ebabce
5
+ SHA512:
6
+ metadata.gz: 3b456cbd8bc8a4064969f0f3182951c779c12b58836d8821f6a1a6ac5737543349c7d16f58ecbeabad18eaec8d9c2a8e81f472a778c65eaedfd59d7c8019c14e
7
+ data.tar.gz: 44cceaca1a35835e7b0d29be5840baf3edd7747c033f2351e60c53d5ad99d47b97696ca161a475dfb05cd792a1fd9ac1d0924bdd763f1ee0e832cc815571ed38
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
+
11
+ .ruby-gemset
12
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flatter-extensions.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Artem Kuzko
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,37 @@
1
+ # Flatter::Extensions
2
+
3
+ A set of extensions to be used with [Flatter](https://github.com/akuzko/flatter) gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'flatter-extensions'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install flatter-extensions
20
+
21
+ ## Usage
22
+
23
+ Coming soon.
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/akuzko/flatter-extensions.
32
+
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
37
+
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/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flatter/extensions"
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
+ require "pry"
10
+ Pry.start
11
+
12
+ # require "irb"
13
+ # 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,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flatter/extensions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flatter-extensions"
8
+ spec.version = Flatter::Extensions::VERSION
9
+ spec.authors = ["Artem Kuzko"]
10
+ spec.email = ["a.kuzko@gmail.com"]
11
+
12
+ spec.summary = %q{Set of extensions for Flatter gem.}
13
+ spec.description = %q{Set of extensions to be used with Flatter gem. They
14
+ provide a number of optional auxiliary functionality, which may be very
15
+ helpful from case to case, especially if using Flatter for mapping
16
+ ActiveRecord objects}
17
+ spec.homepage = "https://github.com/akuzko/flatter-extensions"
18
+ spec.license = "MIT"
19
+
20
+ spec.required_ruby_version = '>= 2.0.0'
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = "bin"
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "flatter"
28
+
29
+ spec.add_development_dependency "activerecord", ">= 4.0"
30
+ spec.add_development_dependency "sqlite3"
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec"
34
+ spec.add_development_dependency "pry"
35
+ spec.add_development_dependency "pry-nav"
36
+ end
@@ -0,0 +1,16 @@
1
+ require "flatter"
2
+
3
+ require "flatter/extensions/version"
4
+
5
+ require "flatter/extensions/multiparam"
6
+ require "flatter/extensions/order"
7
+ require "flatter/extensions/skipping"
8
+
9
+ if defined? ActiveRecord
10
+ require "flatter/extensions/active_record"
11
+ end
12
+
13
+ module Flatter
14
+ module Extensions
15
+ end
16
+ end
@@ -0,0 +1,132 @@
1
+ if defined? ActiveRecord
2
+ module Flatter
3
+ module Extensions
4
+ module ActiveRecord
5
+ extend ::Flatter::Extension
6
+
7
+ module SkipCallbacks
8
+ def save_without_callbacks
9
+ @_saving_without_callbacks = true
10
+ create_or_update
11
+ ensure
12
+ remove_instance_variable('@_saving_without_callbacks')
13
+ end
14
+
15
+ private
16
+
17
+ def _run_save_callbacks
18
+ @_saving_without_callbacks ? yield : super
19
+ end
20
+
21
+ def _run_create_callbacks
22
+ @_saving_without_callbacks ? yield : super
23
+ end
24
+
25
+ def _run_update_callbacks
26
+ @_saving_without_callbacks ? yield : super
27
+ end
28
+ end
29
+
30
+ register_as :active_record
31
+
32
+ depends_on :skipping
33
+
34
+ hooked do
35
+ ::ActiveRecord::Base.send(:prepend, SkipCallbacks)
36
+ end
37
+
38
+ factory.extend do
39
+ def default_target_from(mapper)
40
+ return super unless mapper.ar?
41
+
42
+ target_from_association(mapper.target) || super
43
+ end
44
+ private :default_target_from
45
+
46
+ def target_from_association(target)
47
+ reflection = reflection_from_target(target)
48
+
49
+ return unless reflection.present?
50
+
51
+ case reflection.macro
52
+ when :has_one, :belongs_to
53
+ target.public_send(name) || target.public_send("build_#{name}")
54
+ when :has_many
55
+ target.association(reflection.name).build
56
+ end
57
+ end
58
+ private :target_from_association
59
+
60
+ def reflection_from_target(target)
61
+ target_class = target.class
62
+ reflection = target_class.reflect_on_association(name)
63
+ reflection || target_class.reflect_on_association(name.pluralize)
64
+ end
65
+ private :reflection_from_target
66
+ end
67
+
68
+ mapper.add_options :foreign_key, :mounter_foreign_key do
69
+ def apply(*)
70
+ return super unless ar?
71
+
72
+ ::ActiveRecord::Base.transaction{ super }
73
+ end
74
+
75
+ def save_target
76
+ return super unless ar?
77
+
78
+ assign_foreign_keys_from_mountings
79
+
80
+ was_new = target.new_record?
81
+ result = target.save_without_callbacks
82
+
83
+ if result && was_new
84
+ assign_foreign_keys_for_mountings
85
+ end
86
+
87
+ result != false
88
+ end
89
+ protected :save_target
90
+
91
+ def assign_foreign_keys_from_mountings
92
+ associated_mountings(:mounter_foreign_key).each do |mounting|
93
+ target[mounting.mounter_foreign_key] = mounting.target.id
94
+ end
95
+ end
96
+ private :assign_foreign_keys_from_mountings
97
+
98
+ def assign_foreign_keys_for_mountings
99
+ associated_mountings(:foreign_key).each do |mounting|
100
+ mounting.target[mounting.foreign_key] = target.id
101
+ end
102
+ end
103
+ private :assign_foreign_keys_for_mountings
104
+
105
+ def associated_mountings(key)
106
+ root_mountings.select do |mounting|
107
+ mounter = mounting.mounter
108
+ mounter = mounter.mounter if mounter.trait?
109
+ mounting.options.key?(key) && mounter == self
110
+ end
111
+ end
112
+ private :associated_mountings
113
+
114
+ def skip!
115
+ if ar?
116
+ if target.new_record?
117
+ target.instance_variable_set('@destroyed', true)
118
+ else
119
+ target.restore_attributes
120
+ end
121
+ end
122
+ super
123
+ end
124
+
125
+ def ar?
126
+ target.class < ::ActiveRecord::Base
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,51 @@
1
+ module Flatter
2
+ module Extensions
3
+ module Multiparam
4
+ extend ::Flatter::Extension
5
+
6
+ register_as :multiparam
7
+
8
+ mapping.add_option :multiparam do
9
+ def write(value)
10
+ return super unless multiparam?
11
+
12
+ write!(multiparam.new(*value))
13
+ end
14
+ end
15
+
16
+ mapper.extend do
17
+ def write(params)
18
+ extract_multiparams!(params)
19
+
20
+ super(params)
21
+ end
22
+
23
+ def extract_multiparams!(params)
24
+ mappings.each do |name, mapping|
25
+ next unless mapping.multiparam?
26
+
27
+ param_keys = params.keys.
28
+ select{ |key| key.to_s =~ /#{name}\(\d+[if]\)/ }.
29
+ sort_by{ |key| key.to_s[/\((\d+).*\)/, 1].to_i }
30
+
31
+ next if param_keys.empty?
32
+
33
+ args = param_keys.each_with_object([]) do |key, values|
34
+ value = params.delete key
35
+ type = key[/\(\d+([if]*)\)/, 1]
36
+
37
+ value = if value.present?
38
+ type.blank? ? value : value.send("to_#{type}")
39
+ end
40
+
41
+ values.push value
42
+ end
43
+
44
+ params[name] = args
45
+ end
46
+ end
47
+ private :extract_multiparams!
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ module Flatter
2
+ module Extensions
3
+ module Order
4
+ extend ::Flatter::Extension
5
+
6
+ register_as :order
7
+
8
+ mapper.add_option :index do
9
+ def index
10
+ options[:index] || 0
11
+ end
12
+
13
+ def mappers_chain(context)
14
+ super.sort_by do |mapper|
15
+ index = mapper.index
16
+ index.is_a?(Hash) ? (index[context] || 0) : index
17
+ end
18
+ end
19
+ private :mappers_chain
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module Flatter
2
+ module Extensions
3
+ module Skipping
4
+ extend ::Flatter::Extension
5
+
6
+ register_as :skipping
7
+
8
+ mapper.extend do
9
+ def run_validations!
10
+ if skipped?
11
+ errors.clear
12
+ true
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ def run_save!
19
+ skipped? ? true : super
20
+ end
21
+
22
+ def skip!
23
+ @skipped = true
24
+ end
25
+
26
+ def skipped?
27
+ !!@skipped
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Flatter
2
+ module Extensions
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flatter-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artem Kuzko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: flatter
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-nav
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: |-
126
+ Set of extensions to be used with Flatter gem. They
127
+ provide a number of optional auxiliary functionality, which may be very
128
+ helpful from case to case, especially if using Flatter for mapping
129
+ ActiveRecord objects
130
+ email:
131
+ - a.kuzko@gmail.com
132
+ executables:
133
+ - console
134
+ - setup
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - .gitignore
139
+ - .rspec
140
+ - .travis.yml
141
+ - Gemfile
142
+ - LICENSE.txt
143
+ - README.md
144
+ - Rakefile
145
+ - bin/console
146
+ - bin/setup
147
+ - flatter-extensions.gemspec
148
+ - lib/flatter/extensions.rb
149
+ - lib/flatter/extensions/active_record.rb
150
+ - lib/flatter/extensions/multiparam.rb
151
+ - lib/flatter/extensions/order.rb
152
+ - lib/flatter/extensions/skipping.rb
153
+ - lib/flatter/extensions/version.rb
154
+ homepage: https://github.com/akuzko/flatter-extensions
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: 2.0.0
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 2.1.11
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: Set of extensions for Flatter gem.
178
+ test_files: []