Class: HaveAPI::Common

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/common.rb

Direct Known Subclasses

Action, Resource

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.custom_attrsObject

Returns the value of attribute custom_attrs.



4
5
6
# File 'lib/haveapi/common.rb', line 4

def custom_attrs
  @custom_attrs
end

Class Method Details

.check_build(msg) ⇒ Object



35
36
37
38
39
# File 'lib/haveapi/common.rb', line 35

def check_build(msg)
  yield
rescue StandardError => e
  raise BuildError.new(msg, e)
end

.has_attr(name, default = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/haveapi/common.rb', line 6

def has_attr(name, default = nil)
  @custom_attrs ||= []
  @custom_attrs << name

  instance_variable_set("@#{name}", default)

  self.class.send(:define_method, name) do |value = nil|
    if value.nil?
      instance_variable_get("@#{name}")
    else
      instance_variable_set("@#{name}", value)
    end
  end
end

.inherit_attrs(subclass) ⇒ Object

Called before subclass defines it’s attributes (before has_attr or custom attr setting), so copy defaults from parent and let it override it.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/haveapi/common.rb', line 23

def inherit_attrs(subclass)
  return unless @custom_attrs

  subclass.custom_attrs = []

  @custom_attrs.each do |attr|
    # puts "#{subclass}: Inherit #{attr} = #{instance_variable_get("@#{attr}")}"
    subclass.method(attr).call(instance_variable_get("@#{attr}"))
    subclass.custom_attrs << attr
  end
end