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



41
42
43
44
45
46
47
# File 'lib/tramway/forms/properties.rb', line 41

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

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

  klass.properties + __ancestor_properties(superklass)
end

#__initialize_properties(subclass) ⇒ Object



49
50
51
# File 'lib/tramway/forms/properties.rb', line 49

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

#__propertiesObject



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

def __properties
  (__ancestor_properties + @properties).uniq
end

#__set_properties(attributes) ⇒ Object



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

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

#properties(*attributes) ⇒ Object



27
28
29
# File 'lib/tramway/forms/properties.rb', line 27

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
# File 'lib/tramway/forms/properties.rb', line 9

def property(attribute)
  @properties << attribute

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

    object.public_send(attribute)
  end

  set_method = "#{attribute}="

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

    object.public_send(set_method, value)
  end
end