Class: Milk::Field

Inherits:
Object show all
Defined in:
lib/milk/field.rb

Overview

A field is a data type for part of a component. This way components can be quickly built without coding any logic. The fields take care of the heavy work.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props) ⇒ Field

Store the field configurations



11
12
13
14
15
# File 'lib/milk/field.rb', line 11

def initialize(props)
  props.each do |key, value|
    self.instance_variable_set('@' + key.to_s, value)
  end
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



8
9
10
# File 'lib/milk/field.rb', line 8

def field
  @field
end

Instance Method Details

#form_fieldObject



22
23
24
# File 'lib/milk/field.rb', line 22

def form_field
  @prefix
end

#nameObject

This is the name shown to the user for the field.



18
19
20
# File 'lib/milk/field.rb', line 18

def name
  @label || self.class.to_s.rpartition('::').last
end

#render(component, prefix) ⇒ Object

This is called to render the html for a field’s form



31
32
33
34
35
36
37
# File 'lib/milk/field.rb', line 31

def render(component, prefix)
  @component = component
  @prefix = prefix
  
  name = self.class.to_s.rpartition('::').last.gsub(/([a-z])([A-Z])/) { "#{$1}_#{$2}" }.downcase
  haml("fields/#{name}")
end

#valueObject



26
27
28
# File 'lib/milk/field.rb', line 26

def value
  @component.instance_variable_get('@' + @field.to_s)
end