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.



5
6
7
# File 'lib/standard_config/schema.rb', line 5

def initialize
  @scopes = Concurrent::Map.new
end

Instance Method Details

#cast_value(value, type) ⇒ Object



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
70
71
# File 'lib/standard_config/schema.rb', line 45

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



10
11
12
13
# File 'lib/standard_config/schema.rb', line 10

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

#field_definition(scope_name, field_name) ⇒ Object



35
36
37
38
# File 'lib/standard_config/schema.rb', line 35

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



19
20
21
22
23
24
# File 'lib/standard_config/schema.rb', line 19

def scope(name, &block)
  name_sym = name.to_sym
  builder = scopes.compute_if_absent(name_sym) { ScopeBuilder.new(name_sym) }
  builder.instance_eval(&block) if block_given?
  builder
end

#scopesObject



15
16
17
# File 'lib/standard_config/schema.rb', line 15

def scopes
  @scopes
end

#scopes_with_field(field_name) ⇒ Object

Return an array of scope names that define the given field



41
42
43
# File 'lib/standard_config/schema.rb', line 41

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)


30
31
32
33
# File 'lib/standard_config/schema.rb', line 30

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)


26
27
28
# File 'lib/standard_config/schema.rb', line 26

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