Class: Fidgit::Schema

Inherits:
Object show all
Defined in:
lib/fidgit/schema.rb

Overview

An object that manages Schema values. Usually loaded from a YAML file.

Examples:

schema = Schema.new(YAML.load(file.read('default_schema.yml')))
default_color = schema.default(Element, :disabled, :color)
schema.merge_schema!(YAML.load(file.read('override_schema.yml'))
overridden_color = schema.default(Element, :disabled, :color)

Constant Summary collapse

CONSTANT_PREFIX =
'?'

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • schema (Hash<Symbol => Hash>)

    data containing



15
16
17
18
19
20
# File 'lib/fidgit/schema.rb', line 15

def initialize(schema)
  @constants = {}
  @elements = {}

  merge_schema! schema
end

Instance Method Details

#constant(name) ⇒ Object

Get the constant value associated with name.

Parameters:

  • name (Symbol)

Returns:



75
76
77
# File 'lib/fidgit/schema.rb', line 75

def constant(name)
  @constants[name]
end

#default(klass, names) ⇒ Object

Parameters:

  • klass (Class)

    Class to look for defaults for.

  • names (Symbol, Array<Symbol>)

    Hash names to search for in that class’s schema.

Raises:

  • (ArgumentError)


81
82
83
84
85
86
# File 'lib/fidgit/schema.rb', line 81

def default(klass, names)
  raise ArgumentError, "#{klass} is not a descendent of the #{Element} class" unless klass.ancestors.include? Element
  value = default_internal(klass, Array(names), true)
  raise("Failed to find named value #{names.inspect} for class #{klass}") unless value
  value
end

#merge_constants!(constants_hash) ⇒ Object

Merge in a hash containing constant values. Arrays will be resolved as colors in RGBA or RGB format.

Parameters:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fidgit/schema.rb', line 35

def merge_constants!(constants_hash)
  constants_hash.each_pair do |name, value|
    @constants[name] = case value
    when Array
      case value.size
      when 3 then Gosu::Color.rgb(*value)
      when 4 then Gosu::Color.rgba(*value)
      else
        raise "Colors must be in 0..255, RGB or RGBA array format"
      end
    else
      value
    end
  end

  self
end

#merge_elements!(elements_hash) ⇒ Object

Merge in a hash containing default values for each element.

Parameters:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fidgit/schema.rb', line 56

def merge_elements!(elements_hash)
  elements_hash.each_pair do |klass_names, data|
    klass = Fidgit
    klass_names.to_s.split('::').each do |klass_name|
      klass = klass.const_get klass_name
    end

    raise "elements must be names of classes derived from #{Element}" unless klass.ancestors.include? Fidgit::Element
    @elements[klass] ||= {}
    @elements[klass].deep_merge! data
  end

  self
end

#merge_schema!(schema) ⇒ Object

Merge in a hash containing constant values.

Parameters:

  • constants_hash (Hash<Symbol => Hash>)

    Containing :colors, :constants and :elements hashes.



25
26
27
28
29
30
# File 'lib/fidgit/schema.rb', line 25

def merge_schema!(schema)
  merge_constants!(schema[:constants]) if schema[:constants]
  merge_elements!(schema[:elements]) if schema[:elements]

  self
end