Class: Saphyr::Schema
- Inherits:
-
Object
- Object
- Saphyr::Schema
- Defined in:
- lib/saphyr/schema.rb
Overview
This class is used to encapsulate schema definition.
Instance Attribute Summary collapse
-
#casts ⇒ Object
readonly
Returns the value of attribute casts.
-
#conditionals ⇒ Object
readonly
Returns the value of attribute conditionals.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#root(value) ⇒ Object
readonly
Returns the value of attribute root.
-
#schemas ⇒ Object
readonly
Returns the value of attribute schemas.
-
#strict(value) ⇒ Object
readonly
—————————————————- DSL.
Instance Method Summary collapse
- #cast(field, method) ⇒ Object
- #conditional(cond, &block) ⇒ Object
- #field(name, type, **opts) ⇒ Object
-
#find_schema(name) ⇒ Object
Find local schema definition.
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
- #root_array? ⇒ Boolean
- #root_object? ⇒ Boolean
- #schema(name, &block) ⇒ Object
- #strict? ⇒ Boolean
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
8 9 10 11 |
# File 'lib/saphyr/schema.rb', line 8 def initialize() @strict, @root = true, :object # Default values @fields, @schemas, @conditionals, @casts = {}, {}, [], {} end |
Instance Attribute Details
#casts ⇒ Object (readonly)
Returns the value of attribute casts.
6 7 8 |
# File 'lib/saphyr/schema.rb', line 6 def casts @casts end |
#conditionals ⇒ Object (readonly)
Returns the value of attribute conditionals.
6 7 8 |
# File 'lib/saphyr/schema.rb', line 6 def conditionals @conditionals end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
6 7 8 |
# File 'lib/saphyr/schema.rb', line 6 def fields @fields end |
#root(value) ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/saphyr/schema.rb', line 6 def root @root end |
#schemas ⇒ Object (readonly)
Returns the value of attribute schemas.
6 7 8 |
# File 'lib/saphyr/schema.rb', line 6 def schemas @schemas end |
#strict(value) ⇒ Object (readonly)
—————————————————- DSL
14 15 16 |
# File 'lib/saphyr/schema.rb', line 14 def strict @strict end |
Instance Method Details
#cast(field, method) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/saphyr/schema.rb', line 47 def cast(field, method) method = method.to_s if method.is_a? Symbol if not method.is_a?(Proc) and not method.is_a?(String) raise Saphyr::Error.new "Bad method, must a Proc or Symbol | String" end @casts.store field.to_s, method end |
#conditional(cond, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/saphyr/schema.rb', line 36 def conditional(cond, &block) cond = cond.to_sym if cond.is_a? String if not cond.is_a?(Proc) and not cond.is_a?(Symbol) raise Saphyr::Error.new "Bad condition, must a Proc or Symbol" end schema = Saphyr::Schema.new schema.instance_eval &block @conditionals << [cond, schema] end |
#field(name, type, **opts) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/saphyr/schema.rb', line 22 def field(name, type, **opts) # TODO : What if field 'name' already exists? name_s = name.to_s # Convert Symbol to string # Raise exceptions if field type is not found @fields[name_s] = Saphyr.config.instanciate_field_type type, opts end |
#find_schema(name) ⇒ Object
Find local schema definition
57 58 59 |
# File 'lib/saphyr/schema.rb', line 57 def find_schema(name) @schemas[name] end |
#root_array? ⇒ Boolean
65 66 67 |
# File 'lib/saphyr/schema.rb', line 65 def root_array? @root == :array end |
#root_object? ⇒ Boolean
69 70 71 |
# File 'lib/saphyr/schema.rb', line 69 def root_object? @root == :object end |
#schema(name, &block) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/saphyr/schema.rb', line 29 def schema(name, &block) schema = Saphyr::Schema.new schema.instance_eval &block # TODO : What if schema 'name' already exists? @schemas[name] = schema end |
#strict? ⇒ Boolean
61 62 63 |
# File 'lib/saphyr/schema.rb', line 61 def strict? @strict end |