Module: Subroutine::Fields::ClassMethods

Defined in:
lib/subroutine/fields.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/subroutine/fields.rb', line 61

def method_missing(method_name, *args, &block)
  caster = ::Subroutine::TypeCaster.casters[method_name.to_sym]
  if caster
    options = args.extract_options!
    options[:type] = method_name.to_sym
    args.push(options)
    field(*args, &block)
  else
    super
  end
end

Instance Method Details

#field(*fields) ⇒ Object Also known as: fields

fields can be provided in the following way: field :field1, :field2 field :field3, :field4, default: ‘my default’



27
28
29
30
31
32
33
# File 'lib/subroutine/fields.rb', line 27

def field(*fields)
  options = fields.extract_options!

  fields.each do |f|
    _field(f, options)
  end
end

#inputs_from(*things) ⇒ Object Also known as: fields_from



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/subroutine/fields.rb', line 36

def inputs_from(*things)
  options = things.extract_options!
  excepts = options.key?(:except) ? Array(options.delete(:except)) : nil
  onlys = options.key?(:only) ? Array(options.delete(:only)) : nil

  things.each do |thing|
    thing._fields.each_pair do |field_name, opts|
      next if excepts && excepts.include?(field_name)
      next if onlys && !onlys.include?(field_name)

      if opts[:association]
        include ::Subroutine::Association unless included_modules.include?(::Subroutine::Association)
        association(field_name, opts)
      else
        field(field_name, opts)
      end
    end
  end
end

#respond_to_missing?(method_name, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/subroutine/fields.rb', line 57

def respond_to_missing?(method_name, *args, &block)
  ::Subroutine::TypeCaster.casters.key?(method_name.to_sym) || super
end