Module: HyperResource::Modules::InternalAttributes::ClassMethods

Defined in:
lib/hyper_resource/modules/internal_attributes.rb

Instance Method Summary collapse

Instance Method Details

#_hr_attributesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hyper_resource/modules/internal_attributes.rb', line 32

def _hr_attributes # @private
  [ :root,
    :href,
    :auth,
    :headers,
    :namespace,
    :adapter,
    :token,

    :request,
    :response,
    :body,

    :attributes,
    :links,
    :objects,

    :loaded
  ]
end

#_hr_class_attribute(*names) ⇒ Object

Inheritable class attribute, kinda like in Rails.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hyper_resource/modules/internal_attributes.rb', line 54

def _hr_class_attribute(*names)
  names.map(&:to_sym).each do |name|
    instance_eval <<-EOT
      def #{name}=(val)
        @#{name} = val
      end
      def #{name}
        return @#{name} if defined?(@#{name})
        return superclass.#{name} if superclass.respond_to?(:#{name})
        nil
      end
    EOT
  end
end

#_hr_class_attributesObject



23
24
25
26
27
28
29
30
# File 'lib/hyper_resource/modules/internal_attributes.rb', line 23

def _hr_class_attributes # @private
  [ :root,             ## e.g. 'https://example.com/api/v1'
    :auth,             ## e.g. {:basic => ['username', 'password']}
    :headers,          ## e.g. {'Accept' => 'application/vnd.example+json'}
    :namespace,        ## e.g. 'ExampleAPI', or the class ExampleAPI itself
    :adapter,          ## subclass of HR::Adapter
  ]
end

#_hr_fallback_attribute(*names) ⇒ Object

Instance attributes which fall back to class attributes.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hyper_resource/modules/internal_attributes.rb', line 70

def _hr_fallback_attribute(*names)
  names.map(&:to_sym).each do |name|
    class_eval <<-EOT
      def #{name}=(val)
        @#{name} = val
      end
      def #{name}
        return @#{name} if defined?(@#{name})
        return self.class.#{name} if self.class.respond_to?(:#{name})
        nil
      end
    EOT
  end
end