Class: Yamo::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/yamo/object.rb

Overview

Yamo::Object is a base-class for a data-object, externally stored in YAML, with a Kwalify-schema and optional validation-hooks.

Direct Known Subclasses

Collection, Singleton

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/yamo/object.rb', line 10

def data
  @data
end

Class Method Details

.create_accessors(schema, obj = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yamo/object.rb', line 23

def self.create_accessors(schema, obj=nil)
  # Generate instance accessor methods
  schema.each_pair do |key, value|
    # We either generate methods on the Class instance of this object, or on a simple instance-object.
    receiver = (obj.nil?) ? self : (class << obj ; self; end)
    receiver.send :define_method, key.gsub('-', '_') do
      retval = (obj ? obj[key] : @data[key]) || value["default"]
      if value["type"] == "map"
        retval = retval.nil? ? Hash.new : retval.dup
        Yamo::Object.create_accessors(value["mapping"], retval)
      end
      retval
    end
  end
end

.load_and_validate_file(file, yaml = false) ⇒ Object

Load and validate file. Source is either eval’d or YAML, depending on yaml



40
# File 'lib/yamo/object.rb', line 40

def self.load_and_validate_file(file, yaml=false); load_and_validate_source(file, yaml, true); end

.load_and_validate_source(src, yaml, file) ⇒ Object

Validate src, interpreting it as a file if file is true, or as a String to eval if file is false.



46
47
48
# File 'lib/yamo/object.rb', line 46

def self.load_and_validate_source(src, yaml, file)
  @validator.load_and_validate_source(src, yaml, file)
end

.schema(schema) ⇒ Object



12
13
14
15
16
17
# File 'lib/yamo/object.rb', line 12

def self.schema(schema)
  @validator = Yamo::Validator.new(schema)
  @validator.hook = self

  create_accessors(@validator.schema["mapping"])
end

.validate_data(data, yaml = false) ⇒ Object

Validate data. Source is either eval’d or YAML, depending on yaml



43
# File 'lib/yamo/object.rb', line 43

def self.validate_data(data, yaml=false); load_and_validate_source(data, yaml, false); end

.validatorObject



19
20
21
# File 'lib/yamo/object.rb', line 19

def self.validator
  @validator
end