Class: Sunrise::Config::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sunrise/config/base.rb

Direct Known Subclasses

Association, Export, Field, Form, Group, Index, Model, Show

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abstract_model, parent = nil, options = nil) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
# File 'lib/sunrise/config/base.rb', line 6

def initialize(abstract_model, parent = nil, options = nil)
  @abstract_model = abstract_model
  @parent = parent
  @config_options = (options || {}).symbolize_keys
  @name = (@config_options.delete(:name) || "noname").to_s
end

Instance Attribute Details

#abstract_modelObject (readonly)

Returns the value of attribute abstract_model.



4
5
6
# File 'lib/sunrise/config/base.rb', line 4

def abstract_model
  @abstract_model
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/sunrise/config/base.rb', line 4

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



4
5
6
# File 'lib/sunrise/config/base.rb', line 4

def parent
  @parent
end

Class Method Details

.register_class_option(option_name, &default) ⇒ Object

Register a class option. Class option is a configuration option that stores it’s value within a class object’s instance variable and is accessed by a class method. Both go by the name of the option.



74
75
76
77
# File 'lib/sunrise/config/base.rb', line 74

def self.register_class_option(option_name, &default)
  scope = class << self; self; end;
  self.register_instance_option(option_name, scope, &default)
end

.register_instance_option(option_name, scope = self, &default) ⇒ Object

Register an instance option. Instance option is a configuration option that stores its value within an instance variable and is accessed by an instance method. Both go by the name of the option.



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
# File 'lib/sunrise/config/base.rb', line 23

def register_instance_option(option_name, scope = self, &default)
  unless options = scope.instance_variable_get("@config_options")
    options = scope.instance_variable_set("@config_options", {})
  end

  option_name = option_name.to_s
  options[option_name] = nil

  # If it's a boolean create an alias for it and remove question mark
  if "?" == option_name[-1, 1]
    scope.send(:define_method, "#{option_name.chop!}?") do
      send(option_name)
    end
  end

  # Define getter/setter by the option name
  scope.send(:define_method, option_name) do |*args, &block|
    if !args[0].nil? || block
      # Invocation with args --> This is the declaration of the option, i.e. setter
      instance_variable_set("@#{option_name}_registered", args[0].nil? ? block : args[0])
    else
      # Invocation without args nor block --> It's the use of the option, i.e. getter
      value = instance_variable_get("@#{option_name}_registered")

      case value
      when Proc then
        unless value.lambda?
          # Track recursive invocation with an instance variable. This prevents run-away recursion
          # and allows configurations such as
          # label { "#{label}".upcase }
          # This will use the default definition when called recursively.
          if instance_variable_get("@#{option_name}_recurring")
            value = instance_eval &default
          else
            instance_variable_set("@#{option_name}_recurring", true)
            value = instance_eval &value
            instance_variable_set("@#{option_name}_recurring", false)
          end
        end
      when nil then
        value = instance_eval &default
      end

      value
    end
  end
end

Instance Method Details

#register_instance_option(option_name, &default) ⇒ Object

Register an instance option for this object only



14
15
16
17
# File 'lib/sunrise/config/base.rb', line 14

def register_instance_option(option_name, &default)
  scope = class << self; self; end;
  self.class.register_instance_option(option_name, scope, &default)
end