Module: Tramway::Forms::Properties::ClassMethods

Defined in:
lib/tramway/forms/properties.rb

Overview

A collection of methods that would be using in users forms

Instance Method Summary collapse

Instance Method Details

#__ancestor_properties(klass = superclass) ⇒ Object

:reek:ManualDispatch { enabled: false }



46
47
48
49
50
51
52
# File 'lib/tramway/forms/properties.rb', line 46

def __ancestor_properties(klass = superclass)
  superklass = klass.superclass

  return [] unless superklass.respond_to?(:properties)

  klass.properties + __ancestor_properties(superklass)
end

#__initialize_properties(subclass) ⇒ Object

:reek:UtilityFunction { enabled: false }



55
56
57
# File 'lib/tramway/forms/properties.rb', line 55

def __initialize_properties(subclass)
  subclass.instance_variable_set(:@properties, [])
end

#__propertiesObject



41
42
43
# File 'lib/tramway/forms/properties.rb', line 41

def __properties
  (__ancestor_properties + @properties).uniq
end

#__set_properties(attributes) ⇒ Object



35
36
37
38
39
# File 'lib/tramway/forms/properties.rb', line 35

def __set_properties(attributes)
  attributes.each do |attribute|
    property(attribute)
  end
end

#properties(*attributes) ⇒ Object



31
32
33
# File 'lib/tramway/forms/properties.rb', line 31

def properties(*attributes)
  attributes.any? ? __set_properties(attributes) : __properties
end

#property(attribute) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tramway/forms/properties.rb', line 9

def property(attribute)
  @properties << attribute

  define_method(attribute) do
    if object.respond_to?(attribute)
      object.public_send(attribute)
    else
      raise NoMethodError, "#{self.class}##{attribute} is not defined"
    end
  end

  set_method = "#{attribute}="

  define_method(set_method) do |value|
    if object.respond_to?(set_method)
      object.public_send(set_method, value)
    else
      raise NoMethodError, "#{self.class}##{set_method} is not defined"
    end
  end
end