Class: Fidgit::Schema
Overview
An object that manages Schema values. Usually loaded from a YAML file.
Constant Summary collapse
- CONSTANT_PREFIX =
'?'
Instance Method Summary collapse
-
#constant(name) ⇒ Object
Get the constant value associated with
name
. - #default(klass, names) ⇒ Object
-
#initialize(schema) ⇒ Schema
constructor
A new instance of Schema.
-
#merge_constants!(constants_hash) ⇒ Object
Merge in a hash containing constant values.
-
#merge_elements!(elements_hash) ⇒ Object
Merge in a hash containing default values for each element.
-
#merge_schema!(schema) ⇒ Object
Merge in a hash containing constant values.
Constructor Details
#initialize(schema) ⇒ Schema
Returns a new instance of Schema.
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
.
75 76 77 |
# File 'lib/fidgit/schema.rb', line 75 def constant(name) @constants[name] end |
#default(klass, names) ⇒ Object
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.
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.
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.
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 |