input_source 0.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.
- data/.gitignore +5 -0
- data/CHANGELOG +0 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/input_source.gemspec +20 -0
- data/lib/input_source/dispatch/controller_extension.rb +14 -0
- data/lib/input_source/dispatch/converse_parameters.rb +41 -0
- data/lib/input_source/dispatch/request_extension.rb +13 -0
- data/lib/input_source/engine.rb +4 -0
- data/lib/input_source/models/active_record_extension.rb +33 -0
- data/lib/input_source/models/errors_extension.rb +19 -0
- data/lib/input_source/railtie.rb +23 -0
- data/lib/input_source/version.rb +3 -0
- data/lib/input_source.rb +10 -0
- metadata +62 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Valery Kvon
|
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
|
+
# InputSource
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'input_source'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install input_source
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
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 new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'input_source/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "input_source"
|
8
|
+
gem.version = InputSource::VERSION
|
9
|
+
gem.authors = ["Valery Kvon"]
|
10
|
+
gem.email = ["addagger@gmail.com"]
|
11
|
+
gem.description = %q{Model's attrubutes remembers the field name}
|
12
|
+
gem.summary = %q{Attribute knows the field name. Helpful to improvise with errors handling}
|
13
|
+
gem.homepage = %q{http://vkvon.ru/projects/input_source}
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.licenses = ['MIT']
|
20
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module InputSource
|
2
|
+
module Dispatch
|
3
|
+
module ConverseParameters
|
4
|
+
# Converse parameters
|
5
|
+
|
6
|
+
def converse_parameters(value, prefix = nil, parent = nil)
|
7
|
+
case value
|
8
|
+
when Hash
|
9
|
+
value.each do |k, v|
|
10
|
+
new_prefix = prefix ? "#{prefix}[#{k}]" : k
|
11
|
+
converse_parameters(v, new_prefix, value)
|
12
|
+
end
|
13
|
+
when Array
|
14
|
+
value.each do |e|
|
15
|
+
new_prefix = "#{prefix}[]"
|
16
|
+
converse_parameters(e, new_prefix, value)
|
17
|
+
end
|
18
|
+
else
|
19
|
+
value
|
20
|
+
end
|
21
|
+
value.tap do |v|
|
22
|
+
v.singleton_class.send(:undef_method, :belongs_to) if v.respond_to?(:belongs_to)
|
23
|
+
v.define_singleton_method :belongs_to do
|
24
|
+
parent
|
25
|
+
end
|
26
|
+
v.singleton_class.send(:undef_method, :input_source) if v.respond_to?(:input_source)
|
27
|
+
v.define_singleton_method :input_source do
|
28
|
+
prefix.to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# private
|
34
|
+
#
|
35
|
+
# def normalize_parameters(value)
|
36
|
+
# converse_parameters(super)
|
37
|
+
# end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'input_source/models/errors_extension'
|
2
|
+
|
3
|
+
module InputSource
|
4
|
+
ActiveModel::Errors.send(:include, ErrorsExtension)
|
5
|
+
|
6
|
+
module ActiveRecordExtension
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
def assign_attributes(*args)
|
13
|
+
new_attributes = args.first
|
14
|
+
if new_attributes.respond_to?(:input_source)
|
15
|
+
@input_source = new_attributes.input_source
|
16
|
+
end
|
17
|
+
super(*args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def field_of(attribute)
|
21
|
+
if @input_source && has_attribute?(attribute)
|
22
|
+
"#{@input_source}[#{attribute}]".tap do |input_source|
|
23
|
+
input_source.instance_eval do
|
24
|
+
def to_id
|
25
|
+
self.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module InputSource
|
2
|
+
module ErrorsExtension
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
delegate :field_of, :to => :@base
|
7
|
+
end
|
8
|
+
|
9
|
+
def fields
|
10
|
+
keys.map {|attribute| field_of(attribute)}.compact.tap do |names|
|
11
|
+
names.instance_eval do
|
12
|
+
def ids
|
13
|
+
self.map(&:to_id)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module InputSource
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
config.before_initialize do
|
6
|
+
ActiveSupport.on_load :active_record do
|
7
|
+
require 'input_source/models/active_record_extension'
|
8
|
+
|
9
|
+
# For Rails > 3.2.9: ActiveRecord::AttributeAssignment
|
10
|
+
# For Rails < 3.2.9: ActiveRecord::Base
|
11
|
+
include ActiveRecordExtension
|
12
|
+
end
|
13
|
+
ActiveSupport.on_load :action_controller do
|
14
|
+
require 'input_source/dispatch/converse_parameters'
|
15
|
+
require 'input_source/dispatch/request_extension'
|
16
|
+
require 'input_source/dispatch/controller_extension'
|
17
|
+
ActionDispatch::Request.send(:include, Dispatch::RequestExtension)
|
18
|
+
include Dispatch::ControllerExtension
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/lib/input_source.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: input_source
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Valery Kvon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-31 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Model's attrubutes remembers the field name
|
15
|
+
email:
|
16
|
+
- addagger@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- CHANGELOG
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- input_source.gemspec
|
28
|
+
- lib/input_source.rb
|
29
|
+
- lib/input_source/dispatch/controller_extension.rb
|
30
|
+
- lib/input_source/dispatch/converse_parameters.rb
|
31
|
+
- lib/input_source/dispatch/request_extension.rb
|
32
|
+
- lib/input_source/engine.rb
|
33
|
+
- lib/input_source/models/active_record_extension.rb
|
34
|
+
- lib/input_source/models/errors_extension.rb
|
35
|
+
- lib/input_source/railtie.rb
|
36
|
+
- lib/input_source/version.rb
|
37
|
+
homepage: http://vkvon.ru/projects/input_source
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.8.24
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Attribute knows the field name. Helpful to improvise with errors handling
|
62
|
+
test_files: []
|