Class: AdminAssistant::ColumnConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_assistant/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ ColumnConfig

Returns a new instance of ColumnConfig.



74
75
76
77
78
79
80
# File 'lib/admin_assistant/builder.rb', line 74

def initialize(fields)
  @fields = {}
  fields.each do |fn, value|
    @fields[fn] = value.is_a?(Array) ? value : [value, nil]
  end
  @values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/admin_assistant/builder.rb', line 109

def method_missing(meth, *args, &block)
  if @fields[meth]
    return method_missing_matching_fields_config(meth, block, *args)
  elsif meth.to_s =~ /=$/
    result = method_missing_maybe_setter(meth, *args)
    return result unless result.nil?
  elsif meth.to_s =~ /\?$/
    field_name = find_field_from_method(meth, :boolean) { |fn|
      /^#{fn}\?$/
    }
    return !@values[field_name].nil? if field_name
  end
  super
end

Instance Method Details

#find_field_from_method(meth, target_field_type, &block) ⇒ Object



98
99
100
101
102
# File 'lib/admin_assistant/builder.rb', line 98

def find_field_from_method(meth, target_field_type, &block)
  @fields.detect { |fn, tuple|
    meth.to_s =~ block.call(fn) && tuple.first == target_field_type
  }.first
end

#method_missing_matching_fields_config(meth, block, *args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/admin_assistant/builder.rb', line 82

def method_missing_matching_fields_config(meth, block, *args)
  field_type = @fields[meth].first
  if field_type == :accessor
    default_val_or_proc = @fields[meth].last
    if default_val_or_proc.respond_to?(:call)
      default_val_or_proc.call @values[meth]
    else
      @values[meth].nil? ? default_val_or_proc : @values[meth]
    end
  elsif field_type == :boolean
    @values[meth] = true
  elsif field_type == :block
    block ? (@values[meth] = block) : @values[meth]
  end
end

#method_missing_maybe_setter(meth, *args) ⇒ Object



104
105
106
107
# File 'lib/admin_assistant/builder.rb', line 104

def method_missing_maybe_setter(meth, *args)
  field_name = find_field_from_method(meth, :accessor) { |fn| /^#{fn}=$/ }
  @values[field_name] = args.first if field_name
end