Class: Anvil::Resources::Base
- Inherits:
-
Object
- Object
- Anvil::Resources::Base
- Defined in:
- lib/anvil/resources/base.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
-
.build_from_response(response) ⇒ Object
Helper for building resource instances from API responses.
- .create(attributes = {}, client: nil) ⇒ Object
-
.find(id, client: nil) ⇒ Object
Common API operations.
- .list(params = {}, client: nil) ⇒ Object
-
.with_client(api_key: nil) ⇒ Object
Override in subclasses to provide resource-specific client.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(attributes = {}, client: nil) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
-
#method_missing(method_name, *args) ⇒ Object
ActiveRecord-like attribute accessors.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(attributes = {}, client: nil) ⇒ Base
Returns a new instance of Base.
8 9 10 11 |
# File 'lib/anvil/resources/base.rb', line 8 def initialize(attributes = {}, client: nil) @attributes = symbolize_keys(attributes) @client = client || default_client end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
ActiveRecord-like attribute accessors
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/anvil/resources/base.rb', line 14 def method_missing(method_name, *args) if method_name.to_s.end_with?('=') attribute = method_name.to_s.chomp('=').to_sym attributes[attribute] = args.first elsif attributes.key?(method_name) attributes[method_name] else super end end |
Class Attribute Details
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/anvil/resources/base.rb', line 6 def attributes @attributes end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/anvil/resources/base.rb', line 6 def client @client end |
Class Method Details
.build_from_response(response) ⇒ Object
Helper for building resource instances from API responses
77 78 79 80 81 82 83 |
# File 'lib/anvil/resources/base.rb', line 77 def build_from_response(response) if response.data.is_a?(Array) response.data.map { |item| new(item) } else new(response.data) end end |
.create(attributes = {}, client: nil) ⇒ Object
90 91 92 |
# File 'lib/anvil/resources/base.rb', line 90 def create(attributes = {}, client: nil) raise NotImplementedError, "#{self.class.name}#create must be implemented by subclass" end |
.find(id, client: nil) ⇒ Object
Common API operations
86 87 88 |
# File 'lib/anvil/resources/base.rb', line 86 def find(id, client: nil) raise NotImplementedError, "#{self.class.name}#find must be implemented by subclass" end |
.list(params = {}, client: nil) ⇒ Object
94 95 96 |
# File 'lib/anvil/resources/base.rb', line 94 def list(params = {}, client: nil) raise NotImplementedError, "#{self.class.name}#list must be implemented by subclass" end |
Instance Method Details
#==(other) ⇒ Object
41 42 43 |
# File 'lib/anvil/resources/base.rb', line 41 def ==(other) other.is_a?(self.class) && attributes == other.attributes end |
#inspect ⇒ Object
37 38 39 |
# File 'lib/anvil/resources/base.rb', line 37 def inspect "#<#{self.class.name} #{attributes.inspect}>" end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
25 26 27 |
# File 'lib/anvil/resources/base.rb', line 25 def respond_to_missing?(method_name, include_private = false) method_name.to_s.end_with?('=') || attributes.key?(method_name) || super end |
#to_h ⇒ Object
29 30 31 |
# File 'lib/anvil/resources/base.rb', line 29 def to_h attributes end |
#to_json(*args) ⇒ Object
33 34 35 |
# File 'lib/anvil/resources/base.rb', line 33 def to_json(*args) attributes.to_json(*args) end |