module Stronger::PropertyDefinition::ClassMethods

Public Instance Methods

properties() click to toggle source
# File lib/stronger/property_definition.rb, line 4
def properties
  @properties ||= Array.new
end
property(*names, **opts) click to toggle source
# File lib/stronger/property_definition.rb, line 12
def property(*names, **opts)
  new_props = names.map {|name| Property.new(name, **opts) }
  properties.concat(new_props)
  new_props.each{|p| expose_property(p) unless opts.delete(:private)}
end
property_names() click to toggle source
# File lib/stronger/property_definition.rb, line 8
def property_names
  properties.map(&:name)
end

Private Instance Methods

define_property_method(name, &blk) click to toggle source

Defines a method by the given name using a given block, to which the properties on the instance on which the method was called will be yielded as the first argument, and the arguments given to the called method as the rest of the arguments. PropertyErrors raised due to operations on the yielded properties will be rescued and re-raised with their stack trace origin set to the defined method.

# File lib/stronger/property_definition.rb, line 36
def define_property_method(name, &blk)
  define_method(name) do |*args|
    begin
      blk.call(properties, *args)
    rescue PropertyError => e
      e.set_backtrace caller
      raise e
    end
  end
end
expose_property(property) click to toggle source
# File lib/stronger/property_definition.rb, line 20
def expose_property(property)
  define_property_method(property.name) do |properties|
    properties[property.name]
  end

  define_property_method("#{property.name}=") do |properties, value|
    properties[property.name] = value
  end
end