Class: HelloSign::Resource::BaseResource
- Inherits:
-
Object
- Object
- HelloSign::Resource::BaseResource
- Defined in:
- lib/hello_sign/resource/base_resource.rb
Overview
Store the value of a hash. Use missing_method to create method to access it like an object
Direct Known Subclasses
Account, Embedded, SignatureRequest, Team, Template, UnclaimedDraft
Instance Method Summary collapse
-
#data ⇒ type
raw response data from the server in json.
-
#initialize(hash, key = nil) ⇒ HelloSign::Resource::BaseResource
constructor
recursively convert hash data into BaseResource.
-
#method_missing(method) ⇒ Object
Magic method, give class dynamic methods based on hash keys.
Constructor Details
#initialize(hash, key = nil) ⇒ HelloSign::Resource::BaseResource
recursively convert hash data into BaseResource.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hello_sign/resource/base_resource.rb', line 18 def initialize(hash, key=nil) @raw_data = key ? hash[key] : hash @data = @raw_data.inject({}) do |data, (key, value)| data[key.to_s] = if value.is_a? Hash value = BaseResource.new(value) elsif ((value.is_a? Array) && (value[0].is_a? Hash)) value = value.map {|v| BaseResource.new(v)} else value end data end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
Magic method, give class dynamic methods based on hash keys.
If initialized hash has a key which matches the method name, return value of that key.
Otherwise, return nil
45 46 47 |
# File 'lib/hello_sign/resource/base_resource.rb', line 45 def method_missing(method) @data.key?(method.to_s) ? @data[method.to_s] : nil end |
Instance Method Details
#data ⇒ type
raw response data from the server in json
53 54 55 |
# File 'lib/hello_sign/resource/base_resource.rb', line 53 def data @raw_data end |