Module: H::Fields

Defined in:
lib/h/fields.rb

Overview

Helpers to use in ModalFields hooks to automatically generate _h declarations Examples of use

ModalFields.hook do
  decimal do |model, declaration|
    H::Fields.declare_numeric_field model, declaration
  end

  float do |model, declaration|
    H::Fields.declare_numeric_field model, declaration
  end

  integer do |model, declaration|
    H::Fields.declare_integer_field model, declaration
  end
end

ModalFields.hook do
  all_fields do |model, declaration|
    H::Fields.declare_auto_units_field model, declaration
  end
end

ModalFields.hook do
  all_fields do |model, declaration|
    H::Fields.declare_auto_field_with_units model, declaration
  end
end

Class Method Summary collapse

Class Method Details

.declare_auto_field_with_units(model, declaration) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/h/fields.rb', line 138

def declare_auto_field_with_units(model, declaration)
  case declaration.type
  when :date
    declare_date_field model, declaration
  when :time
    declare_time_field model, declaration
  when :datetime
    declare_datetime_field model, declaration
  when :boolean
    declare_boolean_field model, declaration
  else
    declare_auto_units_field model, declaration
  end
end

.declare_auto_field_without_units(model, declaration) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/h/fields.rb', line 153

def declare_auto_field_without_units(model, declaration)
  case declaration.type
  when :date
    declare_date_field model, declaration
  when :time
    declare_time_field model, declaration
  when :datetime
    declare_datetime_field model, declaration
  when :boolean
    declare_boolean_field model, declaration
  when :integer
    declare_integer_field model, declaration
  when :float, :decimal
    declare_numeric_field model, declaration
  end
end

.declare_auto_units_field(model, declaration) ⇒ Object

This is handy to be used in all_fields to make any field with a :units parameter or a valid units suffix a units_h field (and make other numberic fields _h too); If a field with a suffix corresponding to valid units should not be a measure, a :units=>nil parameter should be added.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/h/fields.rb', line 117

def declare_auto_units_field(model, declaration)
  if declaration.type==:float || declaration.type==:decimal || declaration.type==:integer
    units = declaration.attributes[:units]
    unless declaration.attributes.has_key?(:units)
      units = declaration.name.to_s.split('_').last
    end
    if units && units.to_s=='dms'
      declare_dms_field model, declaration
    elsif units && H::Units.valid?(units)
      declare_units_field model, declaration, units
    else
      raise ArgumentError, "Invalid units #{declaration.attributes[:units]} in declaration of #{model.name}" if declaration.attributes[:units]
      if declaration.type==:integer
        declare_integer_field model, declaration
      else
        declare_numeric_field model, declaration
      end
    end
  end
end

.declare_boolean_field(model, declaration) ⇒ Object



83
84
85
# File 'lib/h/fields.rb', line 83

def declare_boolean_field(model, declaration)
  model.logical_h declaration.name
end

.declare_date_field(model, declaration) ⇒ Object



71
72
73
# File 'lib/h/fields.rb', line 71

def declare_date_field(model, declaration)
  model.date_h declaration.name
end

.declare_datetime_field(model, declaration) ⇒ Object



79
80
81
# File 'lib/h/fields.rb', line 79

def declare_datetime_field(model, declaration)
  model.datetime_h declaration.name
end

.declare_dms_field(model, declaration) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/h/fields.rb', line 87

def declare_dms_field(model, declaration)
  options = declaration.attributes.slice(:precision, :longitude, :latitude)
  if precision = declaration.attributes[:h_precision]
    options[:precision] = precision
  end
  declaration.remove_attributes! :h_precision, :longitude, :latitude
  model.dms_h declaration.name, options
end

.declare_integer_field(model, declaration) ⇒ Object

Integers can also be assigned a precision and presented as generic numbers



49
50
51
52
53
54
55
56
57
# File 'lib/h/fields.rb', line 49

def declare_integer_field(model, declaration)
  precision = declaration.attributes[:h_precision] || declaration.attributes[:precision] || 0
  declaration.remove_attributes! :h_precision, :precision
  if precision==0
    model.integer_h declaration.name
  else
    model.number_h declaration.name, :precision=>precision
  end
end

.declare_latitude_field(model, declaration) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/h/fields.rb', line 105

def declare_latitude_field(model, declaration)
  options = declaration.attributes.slice(:precision).merge(:latitude=>true)
  if precision = declaration.attributes[:h_precision]
    options[:precision] = precision
  end
  declaration.remove_attributes! :h_precision
  model.dms_h declaration.name, options
end

.declare_longitude_field(model, declaration) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/h/fields.rb', line 96

def declare_longitude_field(model, declaration)
  options = declaration.attributes.slice(:precision).merge(:longitude=>true)
  if precision = declaration.attributes[:h_precision]
    options[:precision] = precision
  end
  declaration.remove_attributes! :h_precision
  model.dms_h declaration.name, options
end

.declare_numeric_field(model, declaration) ⇒ Object

# For numeric types, a display precision different from the stored precision can be selected with # the :h_precision attribute:



39
40
41
42
43
44
45
46
# File 'lib/h/fields.rb', line 39

def declare_numeric_field(model, declaration)
  options = declaration.attributes.slice(:precision)
  if precision = declaration.attributes[:h_precision]
    options[:precision] = precision
  end
  declaration.remove_attributes! :h_precision
  model.number_h declaration.name, options
end

.declare_time_field(model, declaration) ⇒ Object



75
76
77
# File 'lib/h/fields.rb', line 75

def declare_time_field(model, declaration)
  model.time_h declaration.name
end

.declare_units_field(model, declaration, suffix_units = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/h/fields.rb', line 59

def declare_units_field(model, declaration, suffix_units=nil)
  options = declaration.attributes.slice(:precision, :units)
  options[:units] ||= suffix_units
  if precision = declaration.attributes[:h_precision]
    options[:precision] = precision
  end
  options[:precision] ||= {'m'=>1, 'mm'=>0, 'cm'=>0, 'km'=>3}[options[:units]] # TODO AppSettings
  declaration.remove_attributes! :h_precision, :units
  # declaration.replace!(:type=>:float)
  model.units_h declaration.name, options[:units], options
end