Module: FlexiModel::Fields

Extended by:
ActiveSupport::Concern
Included in:
FlexiModel
Defined in:
lib/flexi_model/fields.rb

Defined Under Namespace

Modules: ClassMethods Classes: FlexiField

Constant Summary collapse

TYPES =
[:integer, :boolean, :multiple, :decimal, :float, :string, :text,
:datetime, :date, :time, :email, :phone, :address, :location, :attachment]
MULTI_PARAMS_FIELDS =
['datetime', 'date', 'time']

Instance Method Summary collapse

Instance Method Details

#_cast(_value, _type) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/flexi_model/fields.rb', line 217

def _cast(_value, _type)
  return _value if _value.nil?

  case _type
    when :decimal, :float
      _value.to_f
    when :integer, :number
      _value.to_i
    when :multiple
      if _value.is_a?(String)
        YAML.load(_value)
      elsif Array
        _value.map do |v|
          if v.is_a?(Hash)
            if v.values.map(&:blank?).uniq == [true]
              nil
            else
              v
            end
          else
            v
          end
        end.compact
      end
    else
      _value
  end
end

#_default_value(field_name) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/flexi_model/fields.rb', line 261

def _default_value(field_name)
  _field = _flexi_fields_by_name[field_name]
  if _field.default.present?
    _field.value(self)
  else
    nil
  end
end

#_get_value(field_name) ⇒ Object



246
247
248
249
250
# File 'lib/flexi_model/fields.rb', line 246

def _get_value(field_name)
  return _load_value_from_record(field_name) if self._record.present?

  _default_value(field_name)
end

#_load_value_from_record(field_name) ⇒ Object



252
253
254
255
256
257
258
259
# File 'lib/flexi_model/fields.rb', line 252

def _load_value_from_record(field_name)
  val = self._record.value_of(field_name.to_s)
  if val.present?
    val.value
  else
    nil
  end
end

#_nameObject

Return default name from the object instance This accessor could be set through set_flexi_name_field :title



88
89
90
91
92
93
94
95
96
# File 'lib/flexi_model/fields.rb', line 88

def _name
  if self.respond_to?(self.flexi_name_field)
    self.send(self.flexi_name_field)
  else
    raise "Define your name field accessor (which may return the object
          title, name or whatever it describes about the current instance)
          - set_flexi_name_field :different_accessor"
  end
end

#assign_attributes(args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flexi_model/fields.rb', line 45

def assign_attributes(args)
  _all_keys          = args.keys
  _multi_params_keys = _all_keys.select { |k| k.to_s.match(/\([\w]+\)/) }
  _normal_keys       = _all_keys - _multi_params_keys

  # Execute only single parameter accessors
  _normal_keys.each do |k|
    self.send :"#{k.to_s}=", args[k]
  end

  # Execute multi params accessors
  if _multi_params_keys.present?
    _groups = { }
    _multi_params_keys.each do |k|
      _accessor = k.to_s.gsub(/\([\w]+\)/, '')
      _index    = k.to_s.match(/\(([\w]+)\)/)[1].to_i - 1

      _groups[_accessor] ||= []
      _groups[_accessor].insert(_index, args[k])
    end

    _groups.each do |k, values|
      _int_values = values.map(&:to_i)
      _field      = self.flexi_fields.select { |f| f.name.to_s == k }.first

      if _field
        _value = case _field.type
          when 'datetime'
            DateTime.new(*_int_values)
          when 'time'
            Time.new(*([Time.now.year, Time.now.month, Time.now.day] + _int_values))
          when 'date'
            Date.new(*_int_values)
        end

        self.send :"#{k}=", _value
      end
    end
  end
end

#initialize(args = { }) ⇒ Object

Define attribute initialization constructor



40
41
42
43
# File 'lib/flexi_model/fields.rb', line 40

def initialize(args = { })
  @attributes = { }
  assign_attributes(args) if args.present?
end

#to_sObject



98
99
100
# File 'lib/flexi_model/fields.rb', line 98

def to_s
  %{#<#{self.class.name}:#{sprintf '0x%x', self.object_id} #{@attributes.map { |k, v| ":#{k}=>'#{v}'" }.join(' ')}>}
end