Class: HashBuilder
- Inherits:
-
Object
- Object
- HashBuilder
- 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
-
#key_type ⇒ Object
readonly
Returns the value of attribute key_type.
Instance Method Summary collapse
-
#initialize(key_type = String) ⇒ HashBuilder
constructor
A new instance of HashBuilder.
- #method_missing(method_name, *args) ⇒ Object
- #respond_to_missing?(method_name, include_private) ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(key_type = String) ⇒ HashBuilder
Returns a new instance of HashBuilder.
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_type ⇒ Object (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
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_h ⇒ Object
28 29 30 |
# File 'lib/sis_ruby/hash_builder.rb', line 28 def to_h @data end |