Class: IMW::Metadata::Field
Overview
Conceptually, a field is a “slot” for which “records” can have values.
An IMW::Metadata::Field is essentially a Hash that has one required property: a name.
IMW::Metadata::Field.new('id')
#=> { 'name' => 'id' }
But you can declare as many other properties as you want (as long as you include a name
):
IMW::Metadata::Field.new 'name' => 'id', 'type' => :integer, 'title' => "ID", 'description' => "Auto-incremented."
#=> { 'name' => 'id', 'type' => :integer, 'title' > "ID", 'description' => "Auto-incremented." }
Some properties make a field special:
has_many
-
Denotes that this record is in a “has_many” relationship with one or more other records. The corresponding value should be an array
has_one
-
Denotes that this record is in a “has_one” relationship with one or more other records. The corresponding value should be an Array in which each key names the joined record and each value is an Array of fields describing the joined record..
:has_many
and :has_one
properties.
Instance Method Summary collapse
- #associations ⇒ Object
- #flat? ⇒ Boolean
- #hierarchical? ⇒ Boolean (also: #nested?)
-
#initialize(obj) ⇒ Field
constructor
A new instance of Field.
- #titleize ⇒ Object
Methods inherited from Hash
#assoc, #compact, #compact!, #deep_merge, #deep_merge!, #dispatch, #emit, #from_pairs, #keep_merge, #quote_keys_with, #rassoc, #reverse_merge, #reverse_merge!, #slice, #slice!, #terminals, #to_openstruct, zip
Constructor Details
#initialize(obj) ⇒ Field
Returns a new instance of Field.
37 38 39 40 41 42 43 44 45 |
# File 'lib/imw/metadata/field.rb', line 37 def initialize obj super() if obj.is_a?(Hash) || obj.is_a?(Field) merge!(obj) raise IMW::ArgumentError.new("A field must have a name") if obj['name'].blank? else self['name'] = obj.to_s.strip end end |
Instance Method Details
#associations ⇒ Object
60 61 |
# File 'lib/imw/metadata/field.rb', line 60 def associations end |
#flat? ⇒ Boolean
52 53 54 |
# File 'lib/imw/metadata/field.rb', line 52 def flat? ! hierarchical? end |
#hierarchical? ⇒ Boolean Also known as: nested?
47 48 49 |
# File 'lib/imw/metadata/field.rb', line 47 def hierarchical? has_key?('has_many') || has_key?('has_one') end |
#titleize ⇒ Object
56 57 58 |
# File 'lib/imw/metadata/field.rb', line 56 def titleize self['title'] || self['name'].capitalize # FIXME we can do better than this! end |