Module: Clusta::Schema::ClassMethods
- Defined in:
- lib/clusta/schema.rb
Instance Method Summary collapse
- #extra_inputs(name) ⇒ Object
- #field(name, options = {}) ⇒ Object
- #field_names ⇒ Object
- #has_non_key_field? ⇒ Boolean
- #has_optional_field? ⇒ Boolean
- #inherited(subclass) ⇒ Object
- #key(name, options = {}) ⇒ Object
- #keys ⇒ Object
- #non_key_fields ⇒ Object
- #optional_field ⇒ Object
Instance Method Details
#extra_inputs(name) ⇒ Object
33 34 35 |
# File 'lib/clusta/schema.rb', line 33 def extra_inputs name alias_method name, :extra_inputs end |
#field(name, options = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/clusta/schema.rb', line 66 def field name, ={} raise AmbiguousArgumentsError.new("Cannot define a second optional field #{name} because field #{optional_field[:name]} is already optional.") if has_optional_field? raise SortError.new("The first field defined must be a key that can be sorted on.") if @fields.empty? && (![:key]) raise SortError.new("Cannot define a key field #{name} because some non-key fields have already been defined.") if [:key] && has_non_key_field? raise SortError.new("Key fields (#{name}) cannot have type :geometry") if [:key] && [:type] == :geometry attr_reader name case [:type] when :int define_method "#{name}=" do |val| instance_variable_set("@#{name}", val.to_i) end when :float define_method "#{name}=" do |val| instance_variable_set("@#{name}", val.to_f) end else define_method "#{name}=" do |val| instance_variable_set("@#{name}", val) end end @fields << .merge(:name => name) end |
#field_names ⇒ Object
42 43 44 |
# File 'lib/clusta/schema.rb', line 42 def field_names @fields.map { |field| field[:name].to_s } end |
#has_non_key_field? ⇒ Boolean
50 51 52 |
# File 'lib/clusta/schema.rb', line 50 def has_non_key_field? @fields.any? { |field| ! field[:key] } end |
#has_optional_field? ⇒ Boolean
46 47 48 |
# File 'lib/clusta/schema.rb', line 46 def has_optional_field? @fields.any? { |field| field[:optional] } end |
#inherited(subclass) ⇒ Object
37 38 39 40 |
# File 'lib/clusta/schema.rb', line 37 def inherited(subclass) subclass.instance_variable_set("@fields", @fields.dup) super end |
#key(name, options = {}) ⇒ Object
92 93 94 |
# File 'lib/clusta/schema.rb', line 92 def key name, ={} field name, .merge(:key => true) end |
#keys ⇒ Object
58 59 60 |
# File 'lib/clusta/schema.rb', line 58 def keys @fields.find_all { |field| field[:key] } end |
#non_key_fields ⇒ Object
62 63 64 |
# File 'lib/clusta/schema.rb', line 62 def non_key_fields @fields.find_all { |field| ! field[:key] } end |
#optional_field ⇒ Object
54 55 56 |
# File 'lib/clusta/schema.rb', line 54 def optional_field @fields.detect { |field| field[:optional] } end |