module Cura::Attributes::HasAttributes::ClassMethods

The class methods to be mixed in when included.

Public Instance Methods

attribute(name, options={}, &block) click to toggle source
# 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