Sha256: 69834ccf36f88a4ed0336d2053034e21de58b53a09b8854db0df7bfcfcf3a84f

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

require 'active_support'
require 'active_support/core_ext/string/inflections'
require 'hashie'
require "hungryform/version"
require "hungryform/resolver"
require "hungryform/validator"
require "hungryform/elements"

# HungryForm is an object that manages form creation and validation.
# A sample object could look like this:
# 
# form = HungryForm.new :params => params do
#   page :about_yourself do
#     text_field :first_name, :required => true
#     text_field :last_name, :required => true
#     checkbox :dog, label: "Do you have a dog?"
#   end
#   page :about_your_dog, visible: '{"SET": "about_yourself_dog"}' do
#     text_field :name, :required
#     text_area :what_can_it_do, label: "What can it do?"
#   end
# end
# 
# A form must contain only pages.
# Whenever a specific form error occurres inside the form it raises a HungryFormException
# 
# When a new instance of a HungryForm is created, it uses options[:params] to
# build a structure of itself. The pages with dependencies, that resolve during this
# process will be included in the form.pages array. Pages without dependencies will be allways resolved. 
# The rest of the pages will be ignored
class HungryForm
  HungryFormException = Class.new(StandardError)

  attr_reader :current_page, :pages

  def initialize(options = {}, &block)
    raise HungryFormException, 'No form structure block given' unless block_given?
    @resolver = Resolver.new(options.slice(:params))
    @pages = []
    instance_eval(&block)
  end

  # Create a form page
  def page(name, options = {}, &block)
    page = Page.new(name, nil, @resolver, options, &block)
    pages << page if page.visible?
  end

  # Entire form validation. Loops through the form pages and validates each page
  def valid?
    is_valid = true
    pages.each do |page|
      #Loop through pages to get all errors
      is_valid = false if page.invalid?
    end

    is_valid
  end

  def invalid?
    !valid?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hungryform-0.0.1 lib/hungryform.rb