Module: Ocs::Resources::DynamicDefiners

Included in:
Base
Defined in:
lib/ocs/resources/dynamic_definers.rb

Instance Method Summary collapse

Instance Method Details

#alias_attribute(alias_name, original_name) ⇒ Object



4
5
6
7
# File 'lib/ocs/resources/dynamic_definers.rb', line 4

def alias_attribute(alias_name, original_name)
  alias_method alias_name, original_name
  alias_method :"#{alias_name}=", :"#{original_name}="
end

#define_action(action_name, required: [], optional: [], api_name: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ocs/resources/dynamic_definers.rb', line 9

def define_action(action_name, required: [], optional: [], api_name: nil)
  define_method(action_name) do |special_parameters = {}|
    api = api_name || "#{action_name}#{self.class.name}"
    parameters = action_parameters(required, optional).merge(special_parameters)
    send_and_update(api, parameters)
  end

  define_method(:"#{action_name}!") do |special_parameters = {}|
    send(action_name) || fail(@error)
  end
end

#define_attribute(attribute_name, type:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ocs/resources/dynamic_definers.rb', line 21

def define_attribute(attribute_name, type:)
  define_method(:"#{attribute_name}=") do |value|
    unless [*type].any? { |t| value.is_a?(t) }
      fail AttributeTypeMismatch.new(
        "#{attribute_name} needs to be a #{[*type].join(" or ")}"
      )
    end
    instance_variable_set(:"@#{attribute_name}", value)
  end

  attr_reader attribute_name
end

#delegate_attribute(attribute_name, to:, as:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ocs/resources/dynamic_definers.rb', line 34

def delegate_attribute(attribute_name, to:, as:)
  writer_method_name = :"#{to}_#{as}="
  define_method(writer_method_name) do |value|
    instance =
      public_send(to) ||
      public_send(:"#{to}=", resource_class(to).new(client))
    instance.public_send(:"#{as}=", value)
  end
  delegations[attribute_name] = writer_method_name

  define_method(:"#{to}_#{as}") do
    instance = public_send(to)
    instance && instance.public_send(as)
  end
end

#delegate_attributes(attribute_name, to:) ⇒ Object



50
51
52
# File 'lib/ocs/resources/dynamic_definers.rb', line 50

def delegate_attributes(attribute_name, to:)
  delegations[attribute_name] = :"#{to}="
end

#has_many(attribute_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ocs/resources/dynamic_definers.rb', line 68

def has_many(attribute_name)
  define_method(:"#{attribute_name}=") do |values|
    unless values.is_a?(Array)
      fail AttributeTypeMismatch.new("#{attribute_name} needs to be an Array")
    end

    klass = resource_class(attribute_name.to_s.singularize)
    instances = values.map do |value|
      case value
      when klass
        value
      when Hash
        klass.new(client, value)
      else
        fail AttributeClassMismatch.new(
          "Elements of #{attribute_name} need to be instances of #{klass} or Hash"
        )
      end
    end
    instance_variable_set(:"@#{attribute_name}", instances)
  end

  attr_reader attribute_name
end

#has_one(attribute_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ocs/resources/dynamic_definers.rb', line 54

def has_one(attribute_name)
  define_method(:"#{attribute_name}=") do |value|
    klass = resource_class(attribute_name)
    unless value.instance_of?(klass)
      fail AttributeClassMismatch.new(
        "#{attribute_name} needs to be an instance of #{klass}"
      )
    end
    instance_variable_set(:"@#{attribute_name}", value)
  end

  attr_reader attribute_name
end