Module: Clusta::Schema::ClassMethods

Defined in:
lib/clusta/schema.rb

Instance Method Summary collapse

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, options={}
  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? && (!options[:key])
  raise SortError.new("Cannot define a key field #{name} because some non-key fields have already been defined.") if options[:key] && has_non_key_field?
  raise SortError.new("Key fields (#{name}) cannot have type :geometry")                                          if options[:key] && options[:type] == :geometry
  
  attr_reader name
  
  case options[: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 << options.merge(:name => name)
end

#field_namesObject



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

Returns:

  • (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

Returns:

  • (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, options={}
  field name, options.merge(:key => true)
end

#keysObject



58
59
60
# File 'lib/clusta/schema.rb', line 58

def keys
  @fields.find_all { |field| field[:key] }
end

#non_key_fieldsObject



62
63
64
# File 'lib/clusta/schema.rb', line 62

def non_key_fields
  @fields.find_all { |field| ! field[:key] }
end

#optional_fieldObject



54
55
56
# File 'lib/clusta/schema.rb', line 54

def optional_field
  @fields.detect { |field| field[:optional] }
end