Class: ActiveProject::Resources::BaseResource

Inherits:
Object
  • Object
show all
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

PersistableResource, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, attributes = {}) ⇒ BaseResource

Returns a new instance of BaseResource.

Parameters:

  • adapter (Adapters::Base)

    The adapter instance that fetched/created this resource.

  • attributes (Hash) (defaults to: {})

    A hash of attributes for the resource.



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

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/active_project/resources/base_resource.rb', line 8

def adapter
  @adapter
end

#attributesObject (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_dataObject (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

.membersObject

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

#inspectObject

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.

Returns:

  • (Boolean)


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_hObject



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