Class: MetaTypes::MetaProperties

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/meta_types/meta_properties.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, hsname) ⇒ MetaProperties

Returns a new instance of MetaProperties.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/meta_types/meta_properties.rb', line 9

def initialize(model, hsname)
  self._model = model
  self._hsname = hsname
  self._props = meta_type && meta_type.meta_type_properties.ordered.inject({}) do |h, mp|

    h[mp.sid] = MetaTypes::MetaProperty.new(self, mp.sid)

    self.define_singleton_method mp.sid do
      h[mp.sid].value
    end

    self.define_singleton_method "#{mp.sid}=" do |v|
      h[mp.sid].value = v
    end

    h
  end
end

Instance Attribute Details

#_hsnameObject

Returns the value of attribute _hsname.



6
7
8
# File 'lib/meta_types/meta_properties.rb', line 6

def _hsname
  @_hsname
end

#_modelObject

Returns the value of attribute _model.



5
6
7
# File 'lib/meta_types/meta_properties.rb', line 5

def _model
  @_model
end

#_propsObject

Returns the value of attribute _props.



7
8
9
# File 'lib/meta_types/meta_properties.rb', line 7

def _props
  @_props
end

Class Method Details

.validators_on(sid) ⇒ Object



87
88
89
90
91
# File 'lib/meta_types/meta_properties.rb', line 87

def validators_on(sid)
  MetaTypeProperty[sid].required? ?
    [ActiveModel::Validations::PresenceValidator.new(attributes: [:title])] :
    []
end

Instance Method Details

#[](sid) ⇒ Object



78
79
80
# File 'lib/meta_types/meta_properties.rb', line 78

def [](sid)
  _props[sid.to_s]
end

#column_for_attribute(sid) ⇒ Object



82
83
84
# File 'lib/meta_types/meta_properties.rb', line 82

def column_for_attribute(sid)
  MetaTypeProperty[sid]
end

#eachObject



57
58
59
# File 'lib/meta_types/meta_properties.rb', line 57

def each()
  _props.each { |k,v| yield(v) }
end

#get_value(sid) ⇒ Object



74
75
76
# File 'lib/meta_types/meta_properties.rb', line 74

def get_value(sid)
  _model.send(_hsname).try :[], sid
end

#lengthObject Also known as: count



61
62
63
# File 'lib/meta_types/meta_properties.rb', line 61

def length
  _props.length
end

#meta_typeObject



66
67
68
# File 'lib/meta_types/meta_properties.rb', line 66

def meta_type()
  _model.meta_type
end

#persisted?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/meta_types/meta_properties.rb', line 53

def persisted?()
  false
end

#set_value(sid, val) ⇒ Object



70
71
72
# File 'lib/meta_types/meta_properties.rb', line 70

def set_value(sid, val)
  _model.send "#{_hsname}=", (_model.send("#{_hsname}") || {}).merge(sid => val)
end

#update_attributes(hashh) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/meta_types/meta_properties.rb', line 28

def update_attributes(hashh)
  hash = hashh.dup
  date_keys = hash.keys.select{|k| /\([123]i\)\Z/.match(k) }.map{|k|
    k.split("(").first
  }.uniq.each{|k|
    dats = (1..3).map{|i| hash.delete("#{k}(#{i}i)")}.join("-")
    self.send "#{k}=", dats
  }
  hash.each do |k,v|
    self.send "#{k}=", v
  end
end

#valid?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/meta_types/meta_properties.rb', line 45

def valid?
  inject([]) do |e, v|
    v.required && v.value.blank? ?
      e << "#{v.label} must be present" :
      e
  end.join(", ").presence
end

#validateObject



41
42
43
# File 'lib/meta_types/meta_properties.rb', line 41

def validate
  raise "saa"
end