Class: Rootage::Model

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

Overview

Model is a container for option keys and values. This is a just hash table, but you can check the value is specified by user or not.

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.



485
486
487
# File 'lib/rootage/core.rb', line 485

def initialize
  @__specified__ = Hash.new
end

Instance Method Details

#[](name) ⇒ Object



489
490
491
# File 'lib/rootage/core.rb', line 489

def [](name)
  instance_variable_get("@%s" % name)
end

#[]=(name, val) ⇒ Object



493
494
495
# File 'lib/rootage/core.rb', line 493

def []=(name, val)
  instance_variable_set("@%s" % name, val)
end

#specified?(name) ⇒ Boolean

Return true if the option is specified by user.

Parameters:

  • name (Symbol)

    option key name

Returns:

  • (Boolean)

    true if the option is specified by user



515
516
517
# File 'lib/rootage/core.rb', line 515

def specified?(name)
  !!@__specified__[name]
end

#specify(name, value) ⇒ void

This method returns an undefined value.

Specify the option by user.

Parameters:

  • name (Symbol)

    option key name

  • value (Object)

    option value



504
505
506
507
# File 'lib/rootage/core.rb', line 504

def specify(name, value)
  self[name] = value
  @__specified__[name] = true
end

#to_hashHash

Convert the model into a hash.

Returns:



523
524
525
526
527
528
529
530
# File 'lib/rootage/core.rb', line 523

def to_hash
  instance_variables.each_with_object(Hash.new) do |var, h|
    var = var[1..-1]
    unless var.to_s.start_with?("__")
      h[var] = self[var]
    end
  end
end