Class: ActiveProject::Resources::BaseResource
- Inherits:
-
Object
- Object
- ActiveProject::Resources::BaseResource
- Defined in:
- lib/active_project/resources/base_resource.rb
Overview
Base class for resource objects (Project, Issue, etc.) Provides common initialization and attribute access via method_missing.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
Class Method Summary collapse
-
.def_members(*names) ⇒ Object
Defines expected members for the resource class.
-
.members ⇒ Object
Class method to define expected members (mainly for introspection/documentation).
Instance Method Summary collapse
-
#initialize(adapter, attributes = {}) ⇒ BaseResource
constructor
A new instance of BaseResource.
-
#inspect ⇒ Object
Basic inspection using defined members if available, otherwise attributes.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Method missing for accessing attributes stored in @attributes hash.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Ensure respond_to? works correctly with method_missing.
- #to_h ⇒ Object
Constructor Details
#initialize(adapter, attributes = {}) ⇒ BaseResource
Returns a new instance of BaseResource.
12 13 14 15 16 17 18 19 |
# File 'lib/active_project/resources/base_resource.rb', line 12 def initialize(adapter, attributes = {}) @adapter = adapter # Store raw_data if provided, otherwise duplicate the input attributes @raw_data = attributes.key?(:raw_data) ? attributes[:raw_data] : attributes.dup # Store attributes for method_missing access, remove raw_data key if it exists @attributes = attributes.dup @attributes.delete(:raw_data) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
Method missing for accessing attributes stored in @attributes hash.
29 30 31 32 33 34 35 36 |
# File 'lib/active_project/resources/base_resource.rb', line 29 def method_missing(method_name, *arguments, &block) if @attributes.key?(method_name) # Return attribute value if no arguments are given (getter) arguments.empty? ? @attributes[method_name] : super else super end end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
8 9 10 |
# File 'lib/active_project/resources/base_resource.rb', line 8 def adapter @adapter end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/active_project/resources/base_resource.rb', line 8 def attributes @attributes end |
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
8 9 10 |
# File 'lib/active_project/resources/base_resource.rb', line 8 def raw_data @raw_data end |
Class Method Details
.def_members(*names) ⇒ Object
Defines expected members for the resource class.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_project/resources/base_resource.rb', line 49 def self.def_members(*names) @members ||= [] names.each do |name| sym = name.to_sym @members << sym define_method(sym) { @attributes[sym] } # reader define_method("#{sym}=") do |val| # writer @attributes[sym] = val end end end |
.members ⇒ Object
Class method to define expected members (mainly for introspection/documentation).
44 45 46 |
# File 'lib/active_project/resources/base_resource.rb', line 44 def self.members @members ||= [] end |
Instance Method Details
#inspect ⇒ Object
Basic inspection using defined members if available, otherwise attributes.
22 23 24 25 26 |
# File 'lib/active_project/resources/base_resource.rb', line 22 def inspect members_to_show = self.class.members.empty? ? @attributes.keys : self.class.members attrs_str = members_to_show.map { |m| "#{m}=#{send(m).inspect}" }.join(", ") "#<#{self.class.name} #{attrs_str}>" end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Ensure respond_to? works correctly with method_missing.
39 40 41 |
# File 'lib/active_project/resources/base_resource.rb', line 39 def respond_to_missing?(method_name, include_private = false) @attributes.key?(method_name) || super end |
#to_h ⇒ Object
61 62 63 64 65 |
# File 'lib/active_project/resources/base_resource.rb', line 61 def to_h self.class.members.each_with_object({}) do |name, hash| hash[name] = public_send(name) end end |