Module: Servitium::SubContexts

Defined in:
lib/servitium/sub_contexts.rb

Instance Method Summary collapse

Instance Method Details

#sub_context(name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/servitium/sub_contexts.rb', line 5

def sub_context(name)
  if name.to_s.singularize == name.to_s
    define_method(:"#{name}_attributes=") do |attributes|
      klass = "#{self.class.name}::#{name.to_s.camelize}".safe_constantize

      writer = "#{name}="
      inst = klass.new(attributes)
      inst.supercontext = self

      send writer, inst if respond_to? writer

      @subcontexts ||= {}
      @subcontexts[name] = inst

      inst
    end

    define_method(:"#{name}=") do |attributes|
      klass = "#{self.class.name}::#{name.to_s.camelize}".safe_constantize
      inst = if attributes.is_a? klass
        attributes
      else
        inst = klass.new(attributes)
        inst.supercontext = self
        inst
      end

      instance_variable_set(:"@#{name}", inst)

      @subcontexts ||= {}
      @subcontexts[name] = inst

      inst
    end
  else
    define_method(:"#{name}_attributes=") do |attributes|
      klass = "#{self.class.name}::#{name.to_s.singularize.camelize}".safe_constantize

      if attributes.is_a?(Hash) || attributes.is_a?(ActionController::Parameters)
        keys = attributes.keys
        attributes = (if keys.reject { |k| k == "TEMPLATE" }.all? { |k| k.to_i.to_s == k }
                        attributes.reject do |k|
                          k == "TEMPLATE"
                        end.values
                      end)
      end

      result = []
      attributes.each do |params|
        next if params["_destroy"] == "1" || params[:_destroy] == "1"

        inst = klass.new(params)
        inst.supercontext = self
        result.push(inst)
      end
      writer = "#{name}="
      send writer, result if respond_to? writer

      @subcontexts ||= {}
      @subcontexts[name] = result

      result
    end

    define_method(:"#{name}=") do |attributes|
      klass = "#{self.class.name}::#{name.to_s.singularize.camelize}".safe_constantize
      result = if attributes.is_a?(Array) && attributes.all? { |a| a.instance_of?(klass) }
        attributes
      else
        if attributes.is_a?(Hash) || attributes.is_a?(ActionController::Parameters)
          attributes = (attributes.values if attributes.keys.all? { |k| k.to_i.to_s == k })
        end

        result = []
        attributes.each do |params|
          inst = klass.new(params)
          inst.supercontext = self
          result.push(inst)
        end
        result
      end

      instance_variable_set(:"@#{name}", result)

      @subcontexts ||= {}
      @subcontexts[name] = result

      result
    end
  end

  if name.to_s.singularize == name.to_s
    define_method(name.to_s) do
      instance_variable_get(:"@#{name}") if instance_variable_defined?(:"@#{name}")
    end
  else
    define_method(name.to_s) do
      value = instance_variable_get(:"@#{name}") if instance_variable_defined?(:"@#{name}")
      value ||= []
      value
    end
  end

  @attributes[name.to_s] = ActiveAttr::AttributeDefinition.new(name, {})
end