Class: Warg::Config::VariableSet::Property

Inherits:
Module
  • Object
show all
Defined in:
lib/warg.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.


1345
1346
1347
1348
# File 'lib/warg.rb', line 1345

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

Instance Method Details

#extended(variables_set) ⇒ Object


1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
# File 'lib/warg.rb', line 1350

def extended(variables_set)
  variables_set.define! @name

  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