Module: Ocs::Resources::DynamicDefiners::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#alias_attribute(alias_name, original_name) ⇒ Object



7
8
9
10
# File 'lib/ocs/resources/dynamic_definers.rb', line 7

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



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ocs/resources/dynamic_definers.rb', line 12

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ocs/resources/dynamic_definers.rb', line 24

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



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

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



53
54
55
# File 'lib/ocs/resources/dynamic_definers.rb', line 53

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

#has_many(attribute_name) ⇒ Object



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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ocs/resources/dynamic_definers.rb', line 57

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