Class: MotionPrime::Config

Inherits:
Object
  • Object
show all
Defined in:
motion-prime/config/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Config

Returns a new instance of Config.



5
6
7
# File 'motion-prime/config/config.rb', line 5

def initialize(attributes = {})
  @attributes = attributes || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'motion-prime/config/config.rb', line 107

def method_missing(name, *args, &block)
  if block_given?
    yield self[name]
  else
    name = name.to_s
    if /(.+)\=$/.match(name)
      store($1, args[0])
    elsif /(.+)\?$/.match(name)
      value = self[$1]
      value.present? && !!value
    else
      self[name]
    end
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'motion-prime/config/config.rb', line 3

def attributes
  @attributes
end

Class Method Details

.configure(&block) ⇒ Object



49
50
51
52
# File 'motion-prime/config/config.rb', line 49

def configure(&block)
  @configure_blocks ||= []
  @configure_blocks << block
end

.configure!Object



54
55
56
57
58
59
60
61
62
63
64
# File 'motion-prime/config/config.rb', line 54

def configure!
  @configure_blocks ||= []
  @base_config ||= self.new()
  @configure_blocks.each do |block|
    block.call(@base_config)
  end
  setup_models
  setup_colors
  setup_fonts
  setup_logger
end

.method_missing(name, *args, &block) ⇒ Object



44
45
46
47
# File 'motion-prime/config/config.rb', line 44

def method_missing(name, *args, &block)
  @base_config ||= self.new()
  @base_config.send(name.to_sym, *args, &block)
end

.setup_colorsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'motion-prime/config/config.rb', line 70

def setup_colors
  return unless @base_config
  colors = @base_config.colors.to_hash.inject({}) do |res, (color, value)|
    unless color == :prefix
      unless @base_config.colors.prefix.nil?
        res[:"#{@base_config.colors.prefix}_#{color}"] = value
      end
      res[:"app_#{color}"] = value
    end
    res
  end
  Symbol.css_colors.merge!(colors)
end

.setup_fontsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'motion-prime/config/config.rb', line 84

def setup_fonts
  return unless @base_config
  colors = @base_config.fonts.to_hash.inject({}) do |res, (font, value)|
    if [:system, :bold, :italic, :monospace].include?(value)
      value = Symbol.uifont[value]
    end
    unless font == :prefix
      unless @base_config.fonts.prefix.nil?
        res[:"#{@base_config.fonts.prefix}_#{font}"] = value
      end
      res[:"app_#{font}"] = value
    end
    res
  end
  Symbol.uifont.merge!(colors)
end

.setup_loggerObject



101
102
103
104
# File 'motion-prime/config/config.rb', line 101

def setup_logger
  Prime::Logger.level = @base_config.logger.level
  Prime::Logger.dealloc_items =  @base_config.logger.dealloc_items
end

.setup_modelsObject



66
67
68
# File 'motion-prime/config/config.rb', line 66

def setup_models
  MotionPrime::Store.connect
end

Instance Method Details

#[](key) ⇒ Object



9
10
11
# File 'motion-prime/config/config.rb', line 9

def [](key)
  @attributes.has_key?(key.to_sym) ? fetch(key) : store(key, self.class.new)
end

#fetch(key, default = nil) ⇒ Object



18
19
20
# File 'motion-prime/config/config.rb', line 18

def fetch(key, default = nil)
  @attributes[key.to_sym] || default
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'motion-prime/config/config.rb', line 31

def has_key?(key)
  !self[key].is_a?(self.class)
end

#nil?Boolean Also known as: blank?

Returns:

  • (Boolean)


22
23
24
# File 'motion-prime/config/config.rb', line 22

def nil?
  @attributes.empty?
end

#present?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'motion-prime/config/config.rb', line 27

def present?
  !blank?
end

#store(key, value) ⇒ Object Also known as: []=



13
14
15
# File 'motion-prime/config/config.rb', line 13

def store(key, value)
  @attributes[key.to_sym] = value
end

#to_hashObject



35
36
37
38
39
40
41
# File 'motion-prime/config/config.rb', line 35

def to_hash
  hash = {}
  @attributes.each do |key, value|
    hash[key] = value.is_a?(MotionPrime::Config) ? value.to_hash : value
  end
  hash
end