Class: HashBuilder

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

Overview

Takes methods and builds an internal hash where the method calls are keys, and their parameter is the value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_type = String) ⇒ HashBuilder

Returns a new instance of HashBuilder.

Raises:

  • (ArgumentError)


8
9
10
11
12
# File 'lib/sis_ruby/hash_builder.rb', line 8

def initialize(key_type = String)
  raise ArgumentError.new("Invalid key type '#{key_type}'") unless [String, Symbol].include?(key_type)
  @key_type = key_type
  @data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



15
16
17
18
19
20
# File 'lib/sis_ruby/hash_builder.rb', line 15

def method_missing(method_name, *args)
  value = args.first
  key = (@key_type == String) ? method_name.to_s : method_name.to_sym
  @data[key] = value
  self
end

Instance Attribute Details

#key_typeObject (readonly)

Returns the value of attribute key_type.



6
7
8
# File 'lib/sis_ruby/hash_builder.rb', line 6

def key_type
  @key_type
end

Instance Method Details

#respond_to_missing?(method_name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/sis_ruby/hash_builder.rb', line 23

def respond_to_missing?(method_name, include_private)
  true  # TODO: exclude ancestor methods?
end

#to_hObject



28
29
30
# File 'lib/sis_ruby/hash_builder.rb', line 28

def to_h
  @data
end