Class: ActiveRecord::Bixformer::PlanAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-bixformer/plan_accessor.rb

Instance Method Summary collapse

Constructor Details

#initialize(plan) ⇒ PlanAccessor

Returns a new instance of PlanAccessor.



4
5
6
7
8
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 4

def initialize(plan)
  @plan                      = plan
  @module_constant_of        = {}
  @module_load_namespaces_of = {}
end

Instance Method Details

#entry_attribute_sizeObject



106
107
108
109
110
111
112
113
114
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 106

def entry_attribute_size
  counter = -> (v) do
    (v[:attributes] || {}).keys.size + (v[:associations] || {}).values.inject(0) do |sum, assoc_v|
      sum + counter.call(assoc_v)
    end
  end

  @entry_attribute_size ||= counter.call(compile_config_value(:entry))
end

#find_module_constant(module_type, name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 73

def find_module_constant(module_type, name)
  name = :base unless name

  module_constant = @module_constant_of["#{module_type}/#{name}"]

  return module_constant if module_constant

  namespaces = @module_load_namespaces_of[module_type] ||= module_load_namespaces(module_type)

  namespaces.each do |namespace|
    constant = "#{namespace}::#{name.to_s.camelize}".safe_constantize

    return @module_constant_of["#{module_type}/#{name}"] = constant if constant
  end

  raise ::ArgumentError.new "Not found module named #{name.to_s.camelize} in module_load_namespaces('#{module_type}')"
end

#forObject



14
15
16
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 14

def for
  @plan.class.__bixformer_model
end

#new_module_instance(module_type, name_or_instance, *initializers) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 59

def new_module_instance(module_type, name_or_instance, *initializers)
  name_or_instance = :base unless name_or_instance

  name_or_instance = name_or_instance.to_s if name_or_instance.is_a?(::Symbol)

  return name_or_instance unless name_or_instance.is_a?(::String)

  if initializers.size > 0
    find_module_constant(module_type, name_or_instance).new(*initializers)
  else
    find_module_constant(module_type, name_or_instance).new
  end
end

#parse_to_type_and_options(value) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 91

def parse_to_type_and_options(value)
  value = value.dup if value.is_a?(::Array) || value.is_a?(::Hash)
  type  = value.is_a?(::Array) ? value.shift : value

  arguments = if value.is_a?(::Array) && value.size == 1 && value.first.is_a?(::Hash)
                value.first
              elsif value.is_a?(::Array)
                value
              else
                nil
              end

  [type, arguments]
end

#pickup_value_for(model, config_name, default_value = nil) ⇒ Object



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
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 22

def pickup_value_for(model, config_name, default_value = nil)
  model_names_without_root = (model.parents.map(&:name) + [model.name]).drop(1)

  # 指定された設定の全設定値を取得
  entire_config_value = compile_config_value(config_name)

  if entire_config_value.is_a?(::Hash)
    # Hashなら、with_indifferent_accessしておく
    entire_config_value = entire_config_value.with_indifferent_access
  elsif entire_config_value.is_a?(::Array) && entire_config_value.last.is_a?(::Hash)
    # Arrayで最後の要素がHashなら、with_indifferent_accessしておく
    config_value = entire_config_value.pop

    entire_config_value.push config_value.with_indifferent_access
  end

  # その中から、指定のmodelに対応する設定部分を抽出
  config_value = if config_name == :entry
                   find_entry(entire_config_value, model_names_without_root)
                 else
                   find_nested_config_value(entire_config_value, model_names_without_root)
                 end

  if config_value.is_a?(::Array)
    # Arrayで最後の要素がHashの場合、それは子要素の設定値なので、結果に含めない
    config_value.extract_options!

    # Arrayなら、要素は文字列化しておく
    config_value = config_value.map(&:to_s)
  elsif config_name != :entry && config_value.is_a?(::Hash)
    # 子要素の設定を排除
    config_value = config_value.except(*model.associations.map(&:name))
  end

  config_value || default_value
end

#raw_valueObject



10
11
12
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 10

def raw_value
  @plan
end

#value_of(config_name) ⇒ Object



18
19
20
# File 'lib/activerecord-bixformer/plan_accessor.rb', line 18

def value_of(config_name)
  compile_config_value(config_name).dup
end