Module: Cura::Attributes::HasAttributes::ClassMethods

Defined in:
lib/cura/attributes/has_attributes.rb

Overview

The class methods to be mixed in when included.

Instance Method Summary collapse

Instance Method Details

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cura/attributes/has_attributes.rb', line 7

def attribute(name, options={}, &block)
  options = options.to_h

  if options[:query]
    define_method("#{name}?") { instance_variable_get("@#{name}") }
  else
    attr_reader(name)
  end

  if options[:query]
    define_method("#{name}=") do |value|
      value = instance_exec(value, options, &block) unless block.nil?

      instance_variable_set("@#{name}", !!value)
    end
  else
    define_method("#{name}=") do |value|
      value = instance_exec(value, options, &block) unless block.nil?

      instance_variable_set("@#{name}", value)
    end
  end
end