Class: Orator::OpenStruct
- Inherits:
-
Object
- Object
- Orator::OpenStruct
- Defined in:
- lib/orator/open_struct.rb
Overview
This provides openstruct-like access to a hash behind the scenes. This class does not dynamically define methods on itself.
Instance Attribute Summary collapse
-
#table ⇒ Object
readonly
This returns the table that we’re using.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
This provides access to the table without raising an error.
-
#[]=(key, value) ⇒ Object
This provides access to the table without raising an error.
-
#initialize(hash = {}) ⇒ OpenStruct
constructor
Initialize.
-
#method_missing(method, *args) ⇒ Object
This handles the magic of this class.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
This checks to see if we can respond to the method.
Constructor Details
#initialize(hash = {}) ⇒ OpenStruct
Initialize.
15 16 17 |
# File 'lib/orator/open_struct.rb', line 15 def initialize(hash = {}) @table = hash.dup end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
This handles the magic of this class. If the method doesn’t end in ‘=`, it checks to see if the element exists in the table; if not, it calls `super`. Otherwise, it sets the value and carries on. It ignores blocks given to it. If too many arguments are passed, it calls `super`.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/orator/open_struct.rb', line 50 def method_missing(method, *args) method = method.to_s raise ArgumentError if args.length > 1 if method =~ /\=\z/ self[method[0..-2]] = args[0] else raise ArgumentError if args.length > 0 super unless @table.key?(method) self[method] end end |
Instance Attribute Details
#table ⇒ Object (readonly)
This returns the table that we’re using.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/orator/open_struct.rb', line 10 class OpenStruct # Initialize. # # @param hash [Hash] the hash to intialize with. def initialize(hash = {}) @table = hash.dup end # This provides access to the table without raising an error. # # @param key [Object] the key of the element we're looking for. # @return [Object, nil] def [](key) @table[key.to_s] end # This provides access to the table without raising an error. # # @param key [Object] the key of the element we're going to set. # @param value [Object] the element we're going to set the key to. def []=(key, value) @table[key.to_s] = value end # This returns the table that we're using. # # @private def table @table.dup end # This handles the magic of this class. If the method doesn't end in `=`, # it checks to see if the element exists in the table; if not, it calls # `super`. Otherwise, it sets the value and carries on. It ignores blocks # given to it. If too many arguments are passed, it calls `super`. # # @param method [Symbol] the method to look up. # @param args [Array<Object>] # @return [Object] def method_missing(method, *args) method = method.to_s raise ArgumentError if args.length > 1 if method =~ /\=\z/ self[method[0..-2]] = args[0] else raise ArgumentError if args.length > 0 super unless @table.key?(method) self[method] end end # This checks to see if we can respond to the method. This is used in # {#respond_to?} to let other objects know we do respond to that method, as # well as ruby internally for the {#method} method. def respond_to_missing?(method, include_private = false) method = method.to_s if method =~ /\=\z/ true else @table.key?(method) end end end |
Instance Method Details
#[](key) ⇒ Object?
This provides access to the table without raising an error.
23 24 25 |
# File 'lib/orator/open_struct.rb', line 23 def [](key) @table[key.to_s] end |
#[]=(key, value) ⇒ Object
This provides access to the table without raising an error.
31 32 33 |
# File 'lib/orator/open_struct.rb', line 31 def []=(key, value) @table[key.to_s] = value end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
This checks to see if we can respond to the method. This is used in #respond_to? to let other objects know we do respond to that method, as well as ruby internally for the #method method.
66 67 68 69 70 71 72 73 74 |
# File 'lib/orator/open_struct.rb', line 66 def respond_to_missing?(method, include_private = false) method = method.to_s if method =~ /\=\z/ true else @table.key?(method) end end |