Module: Epuber::DSL::AttributeSupport

Included in:
Object
Defined in:
lib/epuber/dsl/attribute_support.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, options = {}) ⇒ Object

Method to create attribute for DSL object

Examples:

attribute :name
attribute :title, required: true, inherited: true

Parameters:

  • name (Symbol)

    attribute name

  • options (Hash) (defaults to: {})

Returns:

  • nil

See Also:



19
20
21
22
23
24
25
# File 'lib/epuber/dsl/attribute_support.rb', line 19

def attribute(name, options = {})
  attr = Attribute.new(name, **options)

  dsl_attributes[name] = attr

  define_method_attr(name, attr)
end

#define_method_attr(name, attr) ⇒ Object

Returns nil.

Parameters:

Returns:

  • nil



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/epuber/dsl/attribute_support.rb', line 52

def define_method_attr(name, attr)
  key = name

  # define normal getter
  define_method(key) do
    value = @attributes_values[key]

    if !value.nil?
      # has value -> return it
      value

    elsif attr.inherited? && respond_to?(:parent) && !parent.nil?
      # hasn't value –> try to find it in parent
      parent.send(key)

    elsif !attr.default_value.nil?
      # just return the default value
      attr.converted_value(attr.default_value)
    end
  end

  # define normal setter
  define_method(attr.writer_name) do |value|
    if attr.singularize?
      array_value = if value.is_a? Array
                      value
                    else
                      [value]
                    end

      mapped = array_value.map { |one_value| attr.converted_value(one_value) }

      @attributes_values[key] = mapped
    else
      begin
        @attributes_values[key] = attr.converted_value(value)
      rescue StandardError => e
        UI.warning("Invalid value `#{value}` for attribute `#{name}`, original error `#{e}`",
                   location: caller_locations[1])
      end
    end
  end

  return unless attr.singularize?

  # define singular methods
  singular_key = key.to_s.singularize.to_sym

  define_method(singular_key) do
    value = @attributes_values[key]

    if attr.singularize? && value.is_a?(Array)
      value.first
    else
      value
    end
  end

  define_method(attr.writer_singular_form) do |value|
    if attr.singularize?
      array_value = if value.is_a?(Array)
                      value
                    else
                      [value]
                    end

      @attributes_values[key] = array_value.map { |one_value| attr.converted_value(one_value) }
    else
      @attributes_values[key] = attr.converted_value(value)
    end
  end
end

#dsl_attributesHash<Symbol, Attribute>

All DSL attributes

Returns:



31
32
33
# File 'lib/epuber/dsl/attribute_support.rb', line 31

def dsl_attributes
  @dsl_attributes ||= {}
end

#find_root(instance) ⇒ Object

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/epuber/dsl/attribute_support.rb', line 37

def find_root(instance)
  return unless instance.respond_to?(:parent)

  if instance.parent.nil?
    instance
  else
    find_root(instance.parent)
  end
end