Module: Tembin::Element::FieldDefinationMacros

Included in:
Base
Defined in:
lib/tembin/element/base.rb

Constant Summary collapse

AVAILABLE_TYPES =
[
  :string, :element, :numeric, :array, :boolean, :time
].freeze

Instance Method Summary collapse

Instance Method Details

#field(key, type, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tembin/element/base.rb', line 7

def field(key, type, options = {})
  raise ArgumentError, "Unknown element field type. #{key}, #{type}, #{options}" if !AVAILABLE_TYPES.include?(type)
  define_method(key) do |*args, &block|
    case type
    when :string, :numeric, :boolean
      @attributes[key] = args.first
    when :element
      @attributes[key] = options[:class].build(*args, &block)
    end
  end
end