Class: StandardConfig::Schema

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

Defined Under Namespace

Classes: Drawer, FieldDefinition, ScopeBuilder, ScopedScope

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



3
4
5
# File 'lib/standard_config/schema.rb', line 3

def initialize
  @scopes = {}
end

Instance Method Details

#cast_value(value, type) ⇒ Object



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/standard_config/schema.rb', line 43

def cast_value(value, type)
  return value if value.nil?

  case type
  when :any
    value
  when :string
    value.to_s
  when :integer
    value.to_i
  when :float
    value.to_f
  when :boolean
    case value
    when true, false then value
    when 'true', '1', 1 then true
    when 'false', '0', 0 then false
    else !!value
    end
  when :array
    Array(value)
  when :hash
    value.is_a?(Hash) ? value : {}
  else
    value
  end
end

#draw(&block) ⇒ Object

DSL entry



8
9
10
11
# File 'lib/standard_config/schema.rb', line 8

def draw(&block)
  Drawer.new(self).instance_eval(&block) if block_given?
  self
end

#field_definition(scope_name, field_name) ⇒ Object



33
34
35
36
# File 'lib/standard_config/schema.rb', line 33

def field_definition(scope_name, field_name)
  return nil unless valid_scope?(scope_name)
  scopes[scope_name.to_sym].fields[field_name.to_sym]
end

#scope(name, &block) ⇒ Object



17
18
19
20
21
22
# File 'lib/standard_config/schema.rb', line 17

def scope(name, &block)
  name_sym = name.to_sym
  builder = scopes[name_sym] ||= ScopeBuilder.new(name_sym)
  builder.instance_eval(&block) if block_given?
  builder
end

#scopesObject



13
14
15
# File 'lib/standard_config/schema.rb', line 13

def scopes
  @scopes
end

#scopes_with_field(field_name) ⇒ Object

Return an array of scope names that define the given field



39
40
41
# File 'lib/standard_config/schema.rb', line 39

def scopes_with_field(field_name)
  scopes.keys.select { |s| scopes[s].fields.key?(field_name.to_sym) }
end

#valid_field?(scope_name, field_name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/standard_config/schema.rb', line 28

def valid_field?(scope_name, field_name)
  return false unless valid_scope?(scope_name)
  scopes[scope_name.to_sym].fields.key?(field_name.to_sym)
end

#valid_scope?(name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/standard_config/schema.rb', line 24

def valid_scope?(name)
  scopes.key?(name.to_sym)
end