Class: MotionPrime::Config

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Config

Returns a new instance of Config.



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

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'motion-prime/config/config.rb', line 101

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

Class Method Details

.configure(&block) ⇒ Object



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

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

.configure!Object



48
49
50
51
52
53
54
55
56
57
58
# File 'motion-prime/config/config.rb', line 48

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



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

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

.setup_colorsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'motion-prime/config/config.rb', line 64

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'motion-prime/config/config.rb', line 78

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



95
96
97
98
# File 'motion-prime/config/config.rb', line 95

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

.setup_modelsObject



60
61
62
# File 'motion-prime/config/config.rb', line 60

def setup_models
  MotionPrime::Store.connect
end

Instance Method Details

#[](key) ⇒ Object



7
8
9
# File 'motion-prime/config/config.rb', line 7

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

#fetch(key) ⇒ Object



16
17
18
# File 'motion-prime/config/config.rb', line 16

def fetch(key)
  @attributes[key.to_sym]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'motion-prime/config/config.rb', line 29

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

#nil?Boolean Also known as: blank?

Returns:

  • (Boolean)


20
21
22
# File 'motion-prime/config/config.rb', line 20

def nil?
  @attributes.empty?
end

#present?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'motion-prime/config/config.rb', line 25

def present?
  !blank?
end

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



11
12
13
# File 'motion-prime/config/config.rb', line 11

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

#to_hashObject



33
34
35
# File 'motion-prime/config/config.rb', line 33

def to_hash
  @attributes
end