Class: Schwab::Resources::Base
- Inherits:
-
Object
- Object
- Schwab::Resources::Base
- Defined in:
- lib/schwab/resources/base.rb
Overview
Base class for resource objects that wrap API response hashes Provides Sawyer::Resource-like functionality for method access to hash data
Direct Known Subclasses
Class Method Summary collapse
-
.field_types ⇒ Object
Define fields that should be coerced to specific types Subclasses can override this to specify their field types.
-
.set_field_type(field, type) ⇒ Object
Set field types for automatic coercion.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality comparison.
-
#[](key) ⇒ Object
Hash-style access to data.
-
#[]=(key, value) ⇒ Object
Hash-style setter.
-
#attributes ⇒ Hash
Get attributes as hash.
-
#each {|key, value| ... } ⇒ Object
Iterate over data.
-
#empty? ⇒ Boolean
Check if resource has no data.
-
#initialize(data = {}, client = nil) ⇒ Base
constructor
Initialize a new resource with data.
-
#inspect ⇒ String
Inspect the resource.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Check if key exists.
-
#keys ⇒ Array
Get all keys.
-
#method_missing(method_name, *args, &block) ⇒ Object
Access data via method calls.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if method exists.
-
#to_h ⇒ Hash
(also: #to_hash)
Convert to hash.
-
#to_s ⇒ String
Convert to string.
Constructor Details
#initialize(data = {}, client = nil) ⇒ Base
Initialize a new resource with data
36 37 38 39 40 |
# File 'lib/schwab/resources/base.rb', line 36 def initialize(data = {}, client = nil) @data = data || {} @client = client @_nested_resources = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Access data via method calls
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/schwab/resources/base.rb', line 48 def method_missing(method_name, *args, &block) key = method_name.to_s # Check for setter methods if key.end_with?("=") key = key[0...-1] @data[key.to_sym] = args.first @data[key] = args.first return args.first end # Try symbol key first, then string key if @data.key?(method_name) wrap_value(@data[method_name], method_name) elsif @data.key?(key) wrap_value(@data[key], key.to_sym) else super end end |
Class Method Details
.field_types ⇒ Object
Define fields that should be coerced to specific types Subclasses can override this to specify their field types
22 23 24 |
# File 'lib/schwab/resources/base.rb', line 22 def field_types @field_types ||= {} end |
.set_field_type(field, type) ⇒ Object
Set field types for automatic coercion
27 28 29 |
# File 'lib/schwab/resources/base.rb', line 27 def set_field_type(field, type) field_types[field.to_sym] = type end |
Instance Method Details
#==(other) ⇒ Boolean
Equality comparison
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/schwab/resources/base.rb', line 150 def ==(other) case other when self.class @data == other.to_h when Hash @data == other else false end end |
#[](key) ⇒ Object
Hash-style access to data
87 88 89 90 |
# File 'lib/schwab/resources/base.rb', line 87 def [](key) value = @data[key.to_sym] || @data[key.to_s] wrap_value(value, key.to_sym) end |
#[]=(key, value) ⇒ Object
Hash-style setter
96 97 98 99 |
# File 'lib/schwab/resources/base.rb', line 96 def []=(key, value) @data[key.to_sym] = value @data[key.to_s] = value end |
#attributes ⇒ Hash
Get attributes as hash
128 129 130 |
# File 'lib/schwab/resources/base.rb', line 128 def attributes @data end |
#each {|key, value| ... } ⇒ Object
Iterate over data
164 165 166 |
# File 'lib/schwab/resources/base.rb', line 164 def each(&block) @data.each(&block) end |
#empty? ⇒ Boolean
Check if resource has no data
171 172 173 |
# File 'lib/schwab/resources/base.rb', line 171 def empty? @data.empty? end |
#inspect ⇒ String
Inspect the resource
135 136 137 |
# File 'lib/schwab/resources/base.rb', line 135 def inspect "#<#{self.class.name} #{@data.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?
Check if key exists
105 106 107 |
# File 'lib/schwab/resources/base.rb', line 105 def key?(key) @data.key?(key.to_sym) || @data.key?(key.to_s) end |
#keys ⇒ Array
Get all keys
113 114 115 |
# File 'lib/schwab/resources/base.rb', line 113 def keys @data.keys end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if method exists
74 75 76 77 78 79 80 81 |
# File 'lib/schwab/resources/base.rb', line 74 def respond_to_missing?(method_name, include_private = false) key = method_name.to_s is_setter = key.end_with?("=") key = key[0...-1] if is_setter # Always respond to setters, or check if key exists for getters is_setter || @data.key?(method_name) || @data.key?(key.to_sym) || @data.key?(key) || super end |
#to_h ⇒ Hash Also known as: to_hash
Convert to hash
120 121 122 |
# File 'lib/schwab/resources/base.rb', line 120 def to_h @data end |
#to_s ⇒ String
Convert to string
142 143 144 |
# File 'lib/schwab/resources/base.rb', line 142 def to_s @data.to_s end |