Class: Brock::Schema
- Inherits:
-
Object
- Object
- Brock::Schema
- Defined in:
- lib/brock/schema.rb
Constant Summary collapse
- FIELD_TYPES =
[ IntegerField, BooleanField, TextField, SelectField, StringField ]
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
Instance Method Summary collapse
-
#field_with_name(name) ⇒ Object
private.
-
#initialize(raw_fields) ⇒ Schema
constructor
A new instance of Schema.
- #parse_params(params) ⇒ Object
- #to_html(values = {}) ⇒ Object
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
#fields ⇒ Object (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 |