Class: Brock::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/brock/schema.rb

Constant Summary collapse

FIELD_TYPES =
[
  IntegerField,
  BooleanField,
  TextField,
  SelectField,
  StringField
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_fields) ⇒ Schema

Returns a new instance of Schema.



33
34
35
# File 'lib/brock/schema.rb', line 33

def initialize(raw_fields)
  @fields = raw_fields.map {|raw_field| self.class.field_for(raw_field) }
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



14
15
16
# File 'lib/brock/schema.rb', line 14

def fields
  @fields
end

Class Method Details

.field_for(raw) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/brock/schema.rb', line 24

def self.field_for(raw)
  klass = FIELD_TYPES.detect {|type| type.detect(raw) }
  if klass
    klass.new_from_params(raw)
  else
    raise FieldTypeNotSupported.new(raw)
  end
end

Instance Method Details

#field_with_name(name) ⇒ Object

private



51
52
53
# File 'lib/brock/schema.rb', line 51

def field_with_name(name)
  fields.find {|field| field.name == name.to_sym }
end

#parse_params(params) ⇒ Object



43
44
45
46
47
# File 'lib/brock/schema.rb', line 43

def parse_params(params)
  fields.each_with_object({}) do |field, settings|
    settings[field.name] = field.parse_param(params[field.name])
  end
end

#to_html(values = {}) ⇒ Object



37
38
39
40
41
# File 'lib/brock/schema.rb', line 37

def to_html(values = {})
  fields.map do |field|
    field.to_html(values[field.name.to_s])
  end.join("\n")
end