goaltender 0.0.2

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: 4e1eff5e3ea39c173d9f15e5e76c95355f285c53
4
+ data.tar.gz: 7ca8cad891725bc7efe5ee48b49c82f06abe8462
5
+ SHA512:
6
+ metadata.gz: e3c5d2e7ae36f8fdc07df3b55753954d23609ca02a5aeee9f4eef322fed93a8db69780b66474d59c47db0ae8a171779e2fca6eed140586099393c0cd9348a288
7
+ data.tar.gz: d9fbcfd7f6ef65ebc6db611af5b1c8536a56d8394214f8aaffaa4ad01508ca95db25eac4cad1faf3d41f20af94ac6110161d92a3ad8f6b6fb2d3697d6b90d8a3
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in goaltender.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
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
+ # Goaltender
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/goaltender`. 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 'goaltender'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install goaltender
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 `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` to 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
+ 1. Fork it ( https://github.com/[my-github-username]/goaltender/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "goaltender"
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,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,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'goaltender/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "goaltender"
8
+ spec.version = Goaltender::VERSION
9
+ spec.authors = ["Ryan Francis"]
10
+ spec.email = ["ryan.p.francis@gmail.com"]
11
+
12
+ spec.summary = %q{Rails form parser}
13
+ spec.description = %q{Framework for intuitively parsing Rails forms}
14
+ spec.homepage = "https://github.com/francirp/goaltender"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.9"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
@@ -0,0 +1,95 @@
1
+ module Goaltender
2
+ class Base
3
+
4
+ include ActiveModel::Model
5
+ include Goaltender
6
+
7
+ attr_reader :params, :inputs
8
+ attr_accessor :inputs
9
+
10
+ def initialize(args)
11
+ @params = HashWithIndifferentAccess.new(args)
12
+ @inputs = set_inputs
13
+ after_init(@params)
14
+ end
15
+
16
+ def to_h
17
+ hash = {}
18
+ inputs.each do |input|
19
+ value = instance_variable_get("@#{input.variable_name}")
20
+ hash[input.variable_name] = value
21
+ end
22
+ hash
23
+ end
24
+
25
+ def save
26
+ before_save
27
+ success = valid_form? ? persist! : false
28
+ if success
29
+ after_save
30
+ true
31
+ else
32
+ false
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def set_inputs
39
+ current_class_name = self.class.name
40
+ relevant_classes = [current_class_name]
41
+ classes = current_class_name.split("::")
42
+ while classes.present?
43
+ classes.pop
44
+ possibility = classes.join("::")
45
+ relevant_classes << possibility unless possibility.exclude?("::")
46
+ current_class_name = possibility
47
+ end
48
+
49
+ all_inputs = []
50
+ Goaltender::Base.input_definitions.each do |class_name, form_inputs|
51
+ if relevant_classes.include?(class_name)
52
+ form_inputs.each do |form_input|
53
+ name = form_input[0]
54
+ type = form_input[1]
55
+ options = form_input[2]
56
+ input_value = params[name]
57
+ input = Goaltender::Input.new(self, name, type, input_value, options)
58
+ self.class.__send__(:attr_accessor, input.variable_name)
59
+ instance_variable_set("@#{input.variable_name.to_s}", input.parsed_value)
60
+ all_inputs << input
61
+ end
62
+ end
63
+ end
64
+ all_inputs
65
+ end
66
+
67
+ def valid_form?
68
+ valid? && object.valid?
69
+ end
70
+
71
+ def persist!
72
+ object.save
73
+ end
74
+
75
+ # METHODS FOR CHILDREN
76
+
77
+ def after_init(args)
78
+ # this method is optionally implemented by children to
79
+ # override default initialization behavior
80
+ end
81
+
82
+ def object
83
+ # optionally implemented by children
84
+ end
85
+
86
+ def after_save
87
+ # optionally implemented by children
88
+ end
89
+
90
+ def before_save
91
+ # optionally implemented by children
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,47 @@
1
+ module Goaltender
2
+ class Input
3
+
4
+ attr_accessor :current_instance, :name, :type, :parse_format, :default, :form_path, :form_class, :input_value, :variable_name
5
+
6
+ def initialize(current_instance, name, type, input_value, options = {})
7
+ @current_instance = current_instance
8
+ @name = name
9
+ @default = perform_default(options[:default])
10
+ @input_value = input_value || @default
11
+ @type = type || input_value.class
12
+ @parse_format = options[:parse_format]
13
+ @form_path = options[:form_path]
14
+ @form_class = generate_form_class
15
+ @variable_name = @name
16
+ end
17
+
18
+ def value_parser
19
+ @value_parser ||= "Goaltender::ValueParser::#{type.to_s.classify}".constantize.new({
20
+ input_value: input_value,
21
+ parse_format: parse_format,
22
+ form_class: form_class,
23
+ variable_name: variable_name
24
+ })
25
+ end
26
+
27
+ def parsed_value
28
+ @parsed_value ||= value_parser.parse
29
+ end
30
+
31
+ def generate_form_class
32
+ return nil unless form_path
33
+ folders = form_path.split("/").map(&:classify)
34
+ classify_path = folders.join("::")
35
+ return "Forms::#{classify_path}"
36
+ end
37
+
38
+ def perform_default(default_value)
39
+ if default_value.is_a?(Symbol)
40
+ current_instance.send(default_value)
41
+ else
42
+ default_value
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ module Goaltender
2
+ class ValueParser
3
+ class BelongsTo < ValueParser
4
+
5
+ attr_reader :form_class, :variable_name
6
+
7
+ def after_init(args)
8
+ @form_class = args[:form_class]
9
+ @variable_name = args[:variable_name]
10
+ end
11
+
12
+ def parse
13
+ return input_value unless input_value.present?
14
+ form_class.constantize.new(input_value).to_h
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module Goaltender
2
+ class ValueParser
3
+ class Boolean < ValueParser
4
+
5
+ def parse
6
+ return false unless input_value.present?
7
+ return true if input_value == "1"
8
+ return true if input_value == true
9
+ return true if input_value == "true"
10
+ return false
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module Goaltender
2
+ class ValueParser
3
+ class Date < ValueParser
4
+
5
+ attr_reader :input_format
6
+
7
+ def after_init(args)
8
+ @input_format = args[:input_format] || "%m/%d/%Y"
9
+ end
10
+
11
+ def parse
12
+ return input_value unless input_value.present?
13
+ return input_value if input_value.is_a?(Date)
14
+ Date.strptime(input_value, input_format)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Goaltender
2
+ class ValueParser
3
+ class DateTime < ValueParser
4
+
5
+ attr_reader :input_format
6
+
7
+ def after_init(args)
8
+ @input_format = args[:input_format] || "%m/%d/%Y"
9
+ end
10
+
11
+ def parse
12
+ return input_value unless input_value.present?
13
+ return input_value if input_value.is_a?(DateTime)
14
+ Date.strptime(input_value, input_format)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module Goaltender
2
+ class ValueParser
3
+ class Float < ValueParser
4
+
5
+ REGEX = /(\d|[.])/
6
+
7
+ def parse
8
+ return input_value unless input_value.present?
9
+ return input_value if ["Float", "Fixnum"].include?(input_value.class.name)
10
+ input_value.scan(REGEX).join.try(:to_f)
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ module Goaltender
2
+ class ValueParser
3
+ class HasMany < ValueParser
4
+
5
+ attr_reader :form_class, :variable_name
6
+
7
+ def after_init(args)
8
+ @form_class = args[:form_class]
9
+ @variable_name = args[:variable_name]
10
+ end
11
+
12
+ # should return a pattern like...
13
+ # {
14
+ # loads_attributes: {
15
+ # "0" => {
16
+ # pays_fuel_surcharge: true,
17
+ # pickup_datetime: #datetime_object
18
+ # }
19
+ # }
20
+ # }
21
+ def parse
22
+ return input_value unless input_value.present?
23
+ hash = {}
24
+ input_value.each do |index, obj_hash|
25
+ object_hash = form_class.constantize.new(obj_hash).to_h
26
+ hash[index] = object_hash
27
+ end
28
+ hash
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ module Goaltender
2
+ class ValueParser
3
+ class Integer < ValueParser
4
+
5
+ REGEX = /(\d|[.])/
6
+
7
+ def parse
8
+ return 0 unless input_value.present?
9
+ return input_value if input_value.is_a?(Fixnum)
10
+ return input_value.to_i if input_value.is_a?(Float)
11
+ input_value.scan(REGEX).join.try(:to_i)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module Goaltender
2
+ class ValueParser
3
+ class String < ValueParser
4
+
5
+ def parse
6
+ input_value.try(:to_s)
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ module Goaltender
2
+ class ValueParser
3
+
4
+ attr_accessor :input_value
5
+
6
+ def initialize(args = {})
7
+ @input_value = args[:input_value]
8
+ after_init(args)
9
+ end
10
+
11
+ def parse
12
+ # implemented by child classes
13
+ end
14
+
15
+ def after_init(args)
16
+ # optionally implemented by child classes
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Goaltender
2
+ VERSION = "0.0.2"
3
+ end
data/lib/goaltender.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "goaltender/version"
2
+ require 'goaltender/base'
3
+
4
+ module Goaltender
5
+
6
+ class << self
7
+ def included(base)
8
+ base.extend ClassMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def input(name, type = nil, options = {})
14
+ @@input_definitions ||= {}
15
+ array = @@input_definitions[self.name].present? ? @@input_definitions[self.name] : []
16
+ array << [name, type, options]
17
+ @@input_definitions[self.name] = array
18
+ end
19
+
20
+ def input_definitions
21
+ @@input_definitions
22
+ end
23
+ end
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goaltender
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Francis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-13 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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
+ description: Framework for intuitively parsing Rails forms
42
+ email:
43
+ - ryan.p.francis@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - goaltender.gemspec
58
+ - lib/goaltender.rb
59
+ - lib/goaltender/base.rb
60
+ - lib/goaltender/input.rb
61
+ - lib/goaltender/value_parser.rb
62
+ - lib/goaltender/value_parser/belongs_to.rb
63
+ - lib/goaltender/value_parser/boolean.rb
64
+ - lib/goaltender/value_parser/date.rb
65
+ - lib/goaltender/value_parser/datetime.rb
66
+ - lib/goaltender/value_parser/float.rb
67
+ - lib/goaltender/value_parser/has_many.rb
68
+ - lib/goaltender/value_parser/integer.rb
69
+ - lib/goaltender/value_parser/string.rb
70
+ - lib/goaltender/version.rb
71
+ homepage: https://github.com/francirp/goaltender
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ allowed_push_host: https://rubygems.org
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Rails form parser
96
+ test_files: []