katagami 0.1.0
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +95 -0
- data/Rakefile +81 -0
- data/config/database.yml +7 -0
- data/db/migrate/001_create_test_form_bases.rb +11 -0
- data/katagami.gemspec +32 -0
- data/lib/katagami.rb +17 -0
- data/lib/katagami/field.rb +84 -0
- data/lib/katagami/field/field_option_parser.rb +35 -0
- data/lib/katagami/validation.rb +60 -0
- data/lib/katagami/version.rb +3 -0
- metadata +213 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: a2abb58590f32be6a0ef4521c35051d91cf5af61
         | 
| 4 | 
            +
              data.tar.gz: 3e7f070bdf56023f333fc4bb1924de324a4216ce
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 4a219f322229f67d21f7f9d1397da135ea29c53df6076b489427b7718bb902dc35b43e2154c6787082ef2b727d9ae0819f22e95f022ff223f626b40715334054
         | 
| 7 | 
            +
              data.tar.gz: f702d1a22422e03ca10c84c206c0f7a185ea59d1c7c2659e0ae0a0055fe3be66d30a511559a27ae60e31f791ad55ba56e883790aadf88f71a3e6246821fca8bb
         | 
    
        data/.gitignore
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            MIT License
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Copyright (c) 2017 hshimoyama
         | 
| 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 all
         | 
| 13 | 
            +
            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 THE
         | 
| 21 | 
            +
            SOFTWARE.
         | 
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            The MIT License (MIT)
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Copyright (c) 2017 Shimoyama, Hiroyasu
         | 
| 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,95 @@ | |
| 1 | 
            +
            # Katagami
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            A toolkit for building Form object.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            ## Installation
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Add this line to your application's Gemfile:
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            ```ruby
         | 
| 10 | 
            +
            gem 'katagami'
         | 
| 11 | 
            +
            ```
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            And then execute:
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                $ bundle
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            Or install it yourself as:
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                $ gem install katagami
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            ## Examples
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            Katagami provides 3 way of form field definitions.
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            - Inherits attributes from an ActiveRecord model. (`fields_for`)
         | 
| 26 | 
            +
            - Inherits spcific attributes from an ActiveRecord model. (`field` with a `for:` option)
         | 
| 27 | 
            +
            - Define a field. (`field` with a Class as a field type)
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            ### Inherits attributes from an ActiveRecord model. (`fields_for`)
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            `fields_for` inherits model attributes as form fields and these validators (excludes `id`, `created_at` and `updated_at`.)
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            ```rb
         | 
| 34 | 
            +
            class User < ActiveRecord::Base
         | 
| 35 | 
            +
              # User has attributes 'id', 'name', 'email', 'created_at', 'updated_at'
         | 
| 36 | 
            +
              validates :name, presence: true
         | 
| 37 | 
            +
            end
         | 
| 38 | 
            +
            ```
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            ```rb
         | 
| 41 | 
            +
            class UserForm
         | 
| 42 | 
            +
              fields_for User
         | 
| 43 | 
            +
            end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            UserForm.field_names # => [:name, :email]
         | 
| 46 | 
            +
            UserForm.validators[:name] # => [#<ActiveRecord::Validations::PresenceValidator:0x... @attributes=[:name], @options={}>]
         | 
| 47 | 
            +
            ```
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            fields_for provides `only` and `excludes` options.
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            ```rb
         | 
| 52 | 
            +
            class UserForm
         | 
| 53 | 
            +
              fields_for User, only: :name
         | 
| 54 | 
            +
            end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
            UserForm.field_names # => [:name]
         | 
| 57 | 
            +
            ```
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            ```rb
         | 
| 60 | 
            +
            class UserForm
         | 
| 61 | 
            +
              fields_for User, excludes: :name
         | 
| 62 | 
            +
            end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            UserForm.field_names # => [:email]
         | 
| 65 | 
            +
            ```
         | 
| 66 | 
            +
             | 
| 67 | 
            +
            ### Inherits spcific attributes from an ActiveRecord model. (`field` with a `for:` option)
         | 
| 68 | 
            +
             | 
| 69 | 
            +
            syntax sugar of `fields_for` with `only` option.
         | 
| 70 | 
            +
             | 
| 71 | 
            +
            ```rb
         | 
| 72 | 
            +
            class UserForm
         | 
| 73 | 
            +
              field :name, :email, for: User
         | 
| 74 | 
            +
            end
         | 
| 75 | 
            +
            UserForm.field_names # => [:name, :email]
         | 
| 76 | 
            +
            ```
         | 
| 77 | 
            +
             | 
| 78 | 
            +
            ### Define a field. (`field` with a Class as a field type)
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            ```rb
         | 
| 81 | 
            +
            class UserForm
         | 
| 82 | 
            +
              field :password_confirm, String
         | 
| 83 | 
            +
            end
         | 
| 84 | 
            +
            ```
         | 
| 85 | 
            +
             | 
| 86 | 
            +
            TBD
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            ## Contributing
         | 
| 89 | 
            +
             | 
| 90 | 
            +
            Bug reports and pull requests are welcome on GitHub at https://github.com/hshimoyama/katagami.
         | 
| 91 | 
            +
             | 
| 92 | 
            +
             | 
| 93 | 
            +
            ## License
         | 
| 94 | 
            +
             | 
| 95 | 
            +
            The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
         | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1,81 @@ | |
| 1 | 
            +
            require "bundler/gem_tasks"
         | 
| 2 | 
            +
            require "rspec/core/rake_task"
         | 
| 3 | 
            +
            require 'yaml'
         | 
| 4 | 
            +
            require 'logger'
         | 
| 5 | 
            +
            require 'active_record'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            RSpec::Core::RakeTask.new(:spec)
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            namespace :db do
         | 
| 10 | 
            +
              def create_database config
         | 
| 11 | 
            +
                options = {:charset => 'utf8', :collation => 'utf8_unicode_ci'}
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                create_db = lambda do |config|
         | 
| 14 | 
            +
                  ActiveRecord::Base.establish_connection config.merge('database' => nil)
         | 
| 15 | 
            +
                  ActiveRecord::Base.connection.create_database config['database'], options
         | 
| 16 | 
            +
                  ActiveRecord::Base.establish_connection config
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                begin
         | 
| 20 | 
            +
                  create_db.call config
         | 
| 21 | 
            +
                rescue Mysql::Error => sqlerr
         | 
| 22 | 
            +
                  if sqlerr.errno == 1405
         | 
| 23 | 
            +
                    print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
         | 
| 24 | 
            +
                    root_password = $stdin.gets.strip
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                    grant_statement = <<-SQL
         | 
| 27 | 
            +
                      GRANT ALL PRIVILEGES ON #{config['database']}.*
         | 
| 28 | 
            +
                        TO '#{config['username']}'@'localhost'
         | 
| 29 | 
            +
                        IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;
         | 
| 30 | 
            +
                    SQL
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                    create_db.call config.merge('database' => nil, 'username' => 'root', 'password' => root_password)
         | 
| 33 | 
            +
                  else
         | 
| 34 | 
            +
                    $stderr.puts sqlerr.error
         | 
| 35 | 
            +
                    $stderr.puts "Couldn't create database for #{config.inspect}, charset: utf8, collation: utf8_unicode_ci"
         | 
| 36 | 
            +
                    $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
         | 
| 37 | 
            +
                  end
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              task :environment do
         | 
| 42 | 
            +
                DATABASE_ENV = ENV['DATABASE_ENV'] || 'development'
         | 
| 43 | 
            +
                MIGRATIONS_DIR = ENV['MIGRATIONS_DIR'] || 'db/migrate'
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              task :configuration => :environment do
         | 
| 47 | 
            +
                @config = YAML.load_file('config/database.yml')[DATABASE_ENV]
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              task :configure_connection => :configuration do
         | 
| 51 | 
            +
                ActiveRecord::Base.establish_connection @config
         | 
| 52 | 
            +
                ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
         | 
| 53 | 
            +
              end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
              desc 'Create the database from config/database.yml for the current DATABASE_ENV'
         | 
| 56 | 
            +
              task :create => :configure_connection do
         | 
| 57 | 
            +
                create_database @config
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
              desc 'Drops the database for the current DATABASE_ENV'
         | 
| 61 | 
            +
              task :drop => :configure_connection do
         | 
| 62 | 
            +
                ActiveRecord::Base.connection.drop_database @config['database']
         | 
| 63 | 
            +
              end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
              desc 'Migrate the database (options: VERSION=x, VERBOSE=false).'
         | 
| 66 | 
            +
              task :migrate => :configure_connection do
         | 
| 67 | 
            +
                ActiveRecord::Migration.verbose = true
         | 
| 68 | 
            +
                ActiveRecord::Migrator.migrate MIGRATIONS_DIR, ENV['VERSION'] ? ENV['VERSION'].to_i : nil
         | 
| 69 | 
            +
              end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
              desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
         | 
| 72 | 
            +
              task :rollback => :configure_connection do
         | 
| 73 | 
            +
                step = ENV['STEP'] ? ENV['STEP'].to_i : 1
         | 
| 74 | 
            +
                ActiveRecord::Migrator.rollback MIGRATIONS_DIR, step
         | 
| 75 | 
            +
              end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
              desc "Retrieves the current schema version number"
         | 
| 78 | 
            +
              task :version => :configure_connection do
         | 
| 79 | 
            +
                puts "Current version: #{ActiveRecord::Migrator.current_version}"
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
            end
         | 
    
        data/config/database.yml
    ADDED
    
    
    
        data/katagami.gemspec
    ADDED
    
    | @@ -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 'katagami/version'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |spec|
         | 
| 7 | 
            +
              spec.name          = "katagami"
         | 
| 8 | 
            +
              spec.version       = Katagami::VERSION
         | 
| 9 | 
            +
              spec.authors       = ["hshimoyama"]
         | 
| 10 | 
            +
              spec.email         = ["h.shimoyama@gmail.com"]
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              spec.summary       = "A toolkit for building form objects."
         | 
| 13 | 
            +
              spec.description   = "A toolkit for building form objects."
         | 
| 14 | 
            +
              spec.homepage      = "https://github.com/hshimoyama/katagami"
         | 
| 15 | 
            +
              spec.license       = "MIT"
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
         | 
| 18 | 
            +
              spec.require_paths = ["lib"]
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              spec.add_development_dependency "bundler"
         | 
| 21 | 
            +
              spec.add_development_dependency "rake"
         | 
| 22 | 
            +
              spec.add_development_dependency "rspec"
         | 
| 23 | 
            +
              spec.add_development_dependency "pry"
         | 
| 24 | 
            +
              spec.add_development_dependency "pry-byebug"
         | 
| 25 | 
            +
              spec.add_development_dependency "mysql2"
         | 
| 26 | 
            +
              spec.add_development_dependency "activerecord"
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              spec.add_dependency "axiom"
         | 
| 29 | 
            +
              spec.add_dependency "virtus"
         | 
| 30 | 
            +
              spec.add_dependency "activesupport"
         | 
| 31 | 
            +
              spec.add_dependency "activemodel"
         | 
| 32 | 
            +
            end
         | 
    
        data/lib/katagami.rb
    ADDED
    
    | @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            require "active_model"
         | 
| 2 | 
            +
            require "virtus"
         | 
| 3 | 
            +
            require "katagami/version"
         | 
| 4 | 
            +
            require "katagami/field"
         | 
| 5 | 
            +
            require "katagami/validation"
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            module Katagami
         | 
| 8 | 
            +
              extend ActiveSupport::Concern
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              included do
         | 
| 11 | 
            +
                include Virtus.model
         | 
| 12 | 
            +
                include ActiveModel::Model
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                include Katagami::Field
         | 
| 15 | 
            +
                include Katagami::Validation
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
            end
         | 
| @@ -0,0 +1,84 @@ | |
| 1 | 
            +
            require "axiom.rb"
         | 
| 2 | 
            +
            require "katagami/field/field_option_parser"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module Katagami
         | 
| 5 | 
            +
              module Field
         | 
| 6 | 
            +
                extend ActiveSupport::Concern
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                class_methods do
         | 
| 9 | 
            +
                  def fields_for(model, options = {})
         | 
| 10 | 
            +
                    model = model.to_s.constantize
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                    attribute_names =
         | 
| 13 | 
            +
                      if options[:only].present?
         | 
| 14 | 
            +
                        options[:only]
         | 
| 15 | 
            +
                      else
         | 
| 16 | 
            +
                        options[:excludes] ||= []
         | 
| 17 | 
            +
                        options[:excludes].map(&:to_sym)
         | 
| 18 | 
            +
                        options[:excludes].concat %i[id created_at updated_at]
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                        model.attribute_names.reject do |an|
         | 
| 21 | 
            +
                          options[:excludes].include?(an.to_sym)
         | 
| 22 | 
            +
                        end
         | 
| 23 | 
            +
                      end.map(&:to_sym)
         | 
| 24 | 
            +
                    options = options.slice!(:only, :excludes)
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                    attribute_names.each do |attribute_name|
         | 
| 27 | 
            +
                      field_for_model(attribute_name.to_sym, options.merge(for: model))
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                  def field(*args)
         | 
| 32 | 
            +
                    fop = FieldOptionParser.new(args)
         | 
| 33 | 
            +
                    if fop.type.blank? && for_model?(fop.options)
         | 
| 34 | 
            +
                      fop.fields.each do |f|
         | 
| 35 | 
            +
                        field_for_model(f, fop.options)
         | 
| 36 | 
            +
                      end
         | 
| 37 | 
            +
                    elsif fop.type.present?
         | 
| 38 | 
            +
                      fop.fields.each do |f|
         | 
| 39 | 
            +
                        attribute(f, fop.type, fop.options)
         | 
| 40 | 
            +
                        validates_aggregated(f) if fop.type.include?(Katagami)
         | 
| 41 | 
            +
                      end
         | 
| 42 | 
            +
                    else
         | 
| 43 | 
            +
                      raise ArgumentError, "field requires 'type' or 'for:' argument."
         | 
| 44 | 
            +
                    end
         | 
| 45 | 
            +
                  end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                  def for_model?(options)
         | 
| 48 | 
            +
                    options.instance_of?(Hash) && options&.keys&.include?(:for)
         | 
| 49 | 
            +
                  end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                  def field_for_model(field_name, options)
         | 
| 52 | 
            +
                    model = options[:for].to_s.constantize
         | 
| 53 | 
            +
                    options = options.slice!(:for)
         | 
| 54 | 
            +
                    raise ArgumentError, "Unknown attribute[#{field_name}] for [#{model}]." unless model.attribute_names.include?(field_name.to_s)
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                    type = detect_type(model.attribute_types[field_name.to_s])
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                    attribute(field_name, type, options)
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                    validates_inherited(field_name, from: model)
         | 
| 61 | 
            +
                  end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                  ATTRIBUTE_TYPE_TO_TYPE = {
         | 
| 64 | 
            +
                    boolean: Axiom::Types::Boolean,
         | 
| 65 | 
            +
                    text: String,
         | 
| 66 | 
            +
                    datetime: ActiveSupport::TimeWithZone
         | 
| 67 | 
            +
                  }.freeze
         | 
| 68 | 
            +
                  def detect_type(attribute_type)
         | 
| 69 | 
            +
                    ATTRIBUTE_TYPE_TO_TYPE[attribute_type.type] ||
         | 
| 70 | 
            +
                      attribute_type.type.to_s.camelize.constantize
         | 
| 71 | 
            +
                  end
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                  def field_names
         | 
| 74 | 
            +
                    attribute_set.to_a.map do |attr|
         | 
| 75 | 
            +
                      if validation_aggregated?(attr.name)
         | 
| 76 | 
            +
                        { attr.name => attr.primitive.field_names }
         | 
| 77 | 
            +
                      else
         | 
| 78 | 
            +
                        attr.name
         | 
| 79 | 
            +
                      end
         | 
| 80 | 
            +
                    end
         | 
| 81 | 
            +
                  end
         | 
| 82 | 
            +
                end
         | 
| 83 | 
            +
              end
         | 
| 84 | 
            +
            end
         | 
| @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            module Katagami::Field
         | 
| 2 | 
            +
              class FieldOptionParser
         | 
| 3 | 
            +
                def initialize(args)
         | 
| 4 | 
            +
                  @args = args
         | 
| 5 | 
            +
                end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def has_options?
         | 
| 8 | 
            +
                  @args[-1].instance_of?(Hash)
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def options
         | 
| 12 | 
            +
                  return @args[-1] if has_options?
         | 
| 13 | 
            +
                  {}
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def has_type?
         | 
| 17 | 
            +
                  !type.nil?
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                def type
         | 
| 21 | 
            +
                  if !has_options? && @args[-1].instance_of?(Class)
         | 
| 22 | 
            +
                    @args[-1]
         | 
| 23 | 
            +
                  elsif has_options? && @args[-2].instance_of?(Class)
         | 
| 24 | 
            +
                    @args[-2]
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                def fields
         | 
| 29 | 
            +
                  e = -1
         | 
| 30 | 
            +
                  e -= 1 if has_options?
         | 
| 31 | 
            +
                  e -= 1 if has_type?
         | 
| 32 | 
            +
                  @args[0..e]
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
            end
         | 
| @@ -0,0 +1,60 @@ | |
| 1 | 
            +
            module Katagami
         | 
| 2 | 
            +
              module Validation
         | 
| 3 | 
            +
                extend ActiveSupport::Concern
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                class_methods do
         | 
| 6 | 
            +
                  def validates_aggregated(field_name)
         | 
| 7 | 
            +
                    validate do |record|
         | 
| 8 | 
            +
                      field = record.send(field_name)
         | 
| 9 | 
            +
                      return if field.valid?
         | 
| 10 | 
            +
                      record.errors.add(field_name, field.errors.messages)
         | 
| 11 | 
            +
                    end
         | 
| 12 | 
            +
                    validation_aggregated_fields << field_name.to_sym
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  def validation_aggregated?(field_name)
         | 
| 16 | 
            +
                    validation_aggregated_fields.include?(field_name.to_sym)
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  def validation_aggregated_fields
         | 
| 20 | 
            +
                    @validation_aggregated_fields ||= []
         | 
| 21 | 
            +
                  end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                  def validates_inherited(field_name, from:)
         | 
| 24 | 
            +
                    from = from.to_s.constantize
         | 
| 25 | 
            +
                    from._validators[field_name].each do |validator|
         | 
| 26 | 
            +
                      options = validator.options.dup
         | 
| 27 | 
            +
                      validates_with(
         | 
| 28 | 
            +
                        convert_validator(validator.class),
         | 
| 29 | 
            +
                        options.merge(attributes: [field_name.to_sym])
         | 
| 30 | 
            +
                      )
         | 
| 31 | 
            +
                    end
         | 
| 32 | 
            +
                  end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                  def convert_validator(validator_class)
         | 
| 35 | 
            +
                    if validator_class.to_s.match(/\AActiveRecord::/)
         | 
| 36 | 
            +
                      return validator_class.to_s.gsub('ActiveRecord', 'ActiveModel').constantize
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
                    validator_class
         | 
| 39 | 
            +
                  end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                  def validate_fields(fields)
         | 
| 42 | 
            +
                    of = new(fields)
         | 
| 43 | 
            +
                    of.valid?
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                    fields.each_with_object(FormResult.new(self)) do |(field_name, v), r|
         | 
| 46 | 
            +
                      if validation_aggregated?(field_name)
         | 
| 47 | 
            +
                        field = of.send(field_name)
         | 
| 48 | 
            +
                        r[field_name] = field.class.validate_fields(v).result
         | 
| 49 | 
            +
                      else
         | 
| 50 | 
            +
                        messages = of.errors.messages[field_name.to_sym]
         | 
| 51 | 
            +
                        r[field_name] = {
         | 
| 52 | 
            +
                          status:  messages.present? ? "invalid" : "valid",
         | 
| 53 | 
            +
                          message: messages.present? ? messages.join(",") : nil
         | 
| 54 | 
            +
                        }
         | 
| 55 | 
            +
                      end
         | 
| 56 | 
            +
                    end
         | 
| 57 | 
            +
                  end
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
              end
         | 
| 60 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,213 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: katagami
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - hshimoyama
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2017-05-16 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: '0'
         | 
| 20 | 
            +
              type: :development
         | 
| 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: rake
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - ">="
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '0'
         | 
| 34 | 
            +
              type: :development
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: rspec
         | 
| 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: pry
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - ">="
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: '0'
         | 
| 62 | 
            +
              type: :development
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - ">="
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: '0'
         | 
| 69 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: pry-byebug
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - ">="
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '0'
         | 
| 76 | 
            +
              type: :development
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - ">="
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: mysql2
         | 
| 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: activerecord
         | 
| 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: axiom
         | 
| 113 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 114 | 
            +
                requirements:
         | 
| 115 | 
            +
                - - ">="
         | 
| 116 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 117 | 
            +
                    version: '0'
         | 
| 118 | 
            +
              type: :runtime
         | 
| 119 | 
            +
              prerelease: false
         | 
| 120 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 121 | 
            +
                requirements:
         | 
| 122 | 
            +
                - - ">="
         | 
| 123 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 124 | 
            +
                    version: '0'
         | 
| 125 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 126 | 
            +
              name: virtus
         | 
| 127 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 128 | 
            +
                requirements:
         | 
| 129 | 
            +
                - - ">="
         | 
| 130 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 131 | 
            +
                    version: '0'
         | 
| 132 | 
            +
              type: :runtime
         | 
| 133 | 
            +
              prerelease: false
         | 
| 134 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 135 | 
            +
                requirements:
         | 
| 136 | 
            +
                - - ">="
         | 
| 137 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 138 | 
            +
                    version: '0'
         | 
| 139 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 140 | 
            +
              name: activesupport
         | 
| 141 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 142 | 
            +
                requirements:
         | 
| 143 | 
            +
                - - ">="
         | 
| 144 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 145 | 
            +
                    version: '0'
         | 
| 146 | 
            +
              type: :runtime
         | 
| 147 | 
            +
              prerelease: false
         | 
| 148 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 149 | 
            +
                requirements:
         | 
| 150 | 
            +
                - - ">="
         | 
| 151 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 152 | 
            +
                    version: '0'
         | 
| 153 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 154 | 
            +
              name: activemodel
         | 
| 155 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 156 | 
            +
                requirements:
         | 
| 157 | 
            +
                - - ">="
         | 
| 158 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 159 | 
            +
                    version: '0'
         | 
| 160 | 
            +
              type: :runtime
         | 
| 161 | 
            +
              prerelease: false
         | 
| 162 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 163 | 
            +
                requirements:
         | 
| 164 | 
            +
                - - ">="
         | 
| 165 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 166 | 
            +
                    version: '0'
         | 
| 167 | 
            +
            description: A toolkit for building form objects.
         | 
| 168 | 
            +
            email:
         | 
| 169 | 
            +
            - h.shimoyama@gmail.com
         | 
| 170 | 
            +
            executables: []
         | 
| 171 | 
            +
            extensions: []
         | 
| 172 | 
            +
            extra_rdoc_files: []
         | 
| 173 | 
            +
            files:
         | 
| 174 | 
            +
            - ".gitignore"
         | 
| 175 | 
            +
            - ".rspec"
         | 
| 176 | 
            +
            - Gemfile
         | 
| 177 | 
            +
            - LICENSE
         | 
| 178 | 
            +
            - LICENSE.txt
         | 
| 179 | 
            +
            - README.md
         | 
| 180 | 
            +
            - Rakefile
         | 
| 181 | 
            +
            - config/database.yml
         | 
| 182 | 
            +
            - db/migrate/001_create_test_form_bases.rb
         | 
| 183 | 
            +
            - katagami.gemspec
         | 
| 184 | 
            +
            - lib/katagami.rb
         | 
| 185 | 
            +
            - lib/katagami/field.rb
         | 
| 186 | 
            +
            - lib/katagami/field/field_option_parser.rb
         | 
| 187 | 
            +
            - lib/katagami/validation.rb
         | 
| 188 | 
            +
            - lib/katagami/version.rb
         | 
| 189 | 
            +
            homepage: https://github.com/hshimoyama/katagami
         | 
| 190 | 
            +
            licenses:
         | 
| 191 | 
            +
            - MIT
         | 
| 192 | 
            +
            metadata: {}
         | 
| 193 | 
            +
            post_install_message: 
         | 
| 194 | 
            +
            rdoc_options: []
         | 
| 195 | 
            +
            require_paths:
         | 
| 196 | 
            +
            - lib
         | 
| 197 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 198 | 
            +
              requirements:
         | 
| 199 | 
            +
              - - ">="
         | 
| 200 | 
            +
                - !ruby/object:Gem::Version
         | 
| 201 | 
            +
                  version: '0'
         | 
| 202 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 203 | 
            +
              requirements:
         | 
| 204 | 
            +
              - - ">="
         | 
| 205 | 
            +
                - !ruby/object:Gem::Version
         | 
| 206 | 
            +
                  version: '0'
         | 
| 207 | 
            +
            requirements: []
         | 
| 208 | 
            +
            rubyforge_project: 
         | 
| 209 | 
            +
            rubygems_version: 2.5.2
         | 
| 210 | 
            +
            signing_key: 
         | 
| 211 | 
            +
            specification_version: 4
         | 
| 212 | 
            +
            summary: A toolkit for building form objects.
         | 
| 213 | 
            +
            test_files: []
         |