Class: BoPeep::Config::VariableSet

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

Defined Under Namespace

Classes: Property

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, context) ⇒ VariableSet

Returns a new instance of VariableSet.



1437
1438
1439
1440
1441
1442
# File 'lib/bopeep.rb', line 1437

def initialize(name, context)
  @_name = name
  @context = context
  # FIXME: make this "private" by adding an underscore
  @properties = Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
# File 'lib/bopeep.rb', line 1484

def method_missing(name, *args, &block)
  writer_name = name.to_s
  reader_name = writer_name.chomp("=")

  if ! Property::REGEXP.match?(reader_name)
    super
  elsif reader_name == writer_name && block.nil?
    $stderr.puts "`#{@_name}.#{reader_name}' was accessed before it was defined"
    nil
  elsif writer_name.end_with?("=") or not block.nil?
    value = block || args

    define!(reader_name, *value)
  end
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



1435
1436
1437
# File 'lib/bopeep.rb', line 1435

def context
  @context
end

Instance Method Details

#copy(other) ⇒ Object



1466
1467
1468
1469
1470
1471
1472
# File 'lib/bopeep.rb', line 1466

def copy(other)
  other.properties.each do |property_name|
    value = other.instance_variable_get("@#{property_name}")

    define!(property_name, value)
  end
end

#define!(property_name, value = nil, &block) ⇒ Object



1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
# File 'lib/bopeep.rb', line 1452

def define!(property_name, value = nil, &block)
  property_name = property_name.to_s

  if self.defined?(property_name)
    raise ArgumentError, "`#{@_name}.#{property_name}' is already defined"
  elsif Property::REGEXP.match?(property_name)
    @properties << property_name

    extend Property.new(property_name, block || value)
  else
    raise ArgumentError, "`#{property_name}' is not a valid property name"
  end
end

#defined?(property_name) ⇒ Boolean

Returns:

  • (Boolean)


1448
1449
1450
# File 'lib/bopeep.rb', line 1448

def defined?(property_name)
  @properties.include?(property_name.to_s)
end

#to_hObject



1474
1475
1476
1477
1478
# File 'lib/bopeep.rb', line 1474

def to_h
  @properties.each_with_object({}) do |property_name, variables|
    variables["#{@_name}_#{property_name}"] = send(property_name)
  end
end