Class: BoPeep::Config::VariableSet::Property

Inherits:
Module
  • Object
show all
Defined in:
lib/bopeep.rb

Constant Summary collapse

REGEXP =
/^[A-Za-z_]+$/

Instance Method Summary collapse

Constructor Details

#initialize(name, initial_value = nil) ⇒ Property

Returns a new instance of Property.



1505
1506
1507
1508
# File 'lib/bopeep.rb', line 1505

def initialize(name, initial_value = nil)
  @name = name
  @initial_value = initial_value
end

Instance Method Details

#extended(variables_set) ⇒ Object



1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
# File 'lib/bopeep.rb', line 1510

def extended(variables_set)
  variables_set.singleton_class.class_eval <<-PROPERTY_METHODS
    attr_writer :#{@name}

    def #{@name}(&block)
      if block.nil?
        value = instance_variable_get(:@#{@name})

        if value.respond_to?(:to_proc)
          instance_eval(&value)
        else
          value
        end
      else
        instance_variable_set(:@#{@name}, block)
      end
    end
  PROPERTY_METHODS

  variables_set.instance_variable_set("@#{@name}", @initial_value)
end