class Riddl::Utils::Properties::Backend

Attributes

data[R]
rng[R]
schema[R]

Public Class Methods

new(schema,target,init=nil) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 64
def initialize(schema,target,init=nil)
  @target = target.gsub(/^\/+/,'/')
  @schemas = {}
  @rngs = {}

  if schema.is_a? Hash
    schema.each { |k,v| add_schema k, v }
  elsif schema.is_a? String
    add_schema 'default', schema
  end
  raise "no schemas provided" if @schemas.length == 0
  @schema = @schemas.first[1]
  @rng = @rngs.first[1]

  FileUtils::mkdir_p(File::dirname(@target)) unless File.exist?(@target)
  FileUtils::cp init, @target                if init and not File.exist?(@target)

  raise "properties file not found" unless File.exist?(@target)
  @data = XML::Smart.open_unprotected(@target)
  @data.register_namespace 'p', 'http://riddl.org/ns/common-patterns/properties/1.0'
  @mutex = Mutex.new
end

Public Instance Methods

activate_schema(name) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 87
def activate_schema(name)
  if @schemas[name]
    @schema = @schemas[name]
    @rng = @rngs[name]
    true
  else
    false
  end
end
init_state?(property,new) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 117
def init_state?(property,new)
  @schema.find("boolean(/p:properties/p:#{property}/p:#{new}[position()=1])") || schema.find("boolean(/p:properties/p:optional/p:#{property}/p:#{new}[position()=1])")
end
is_state?(property) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 114
def is_state?(property)
  @schema.find("boolean(/p:properties/p:#{property}[@type='state'])") || schema.find("boolean(/p:properties/p:optional/p:#{property}[@type='state'])")
end
modifiable?(property) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 108
def modifiable?(property)
  @schema.find("boolean(/p:properties/p:#{property}[@modifiable='true'])") || schema.find("boolean(/p:properties/p:optional/p:#{property}[@modifiable='true'])")
end
modify(&block) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 125
def modify(&block)
  tdoc = @data.root.to_doc
  tdoc.register_namespace 'p', 'http://riddl.org/ns/common-patterns/properties/1.0'
  @mutex.synchronize do
    block.call tdoc
    if tdoc.validate_against(@rng){|err| puts err.message }
      block.call @data
      @data.save_as(@target)
      true
    else
      false
    end
  end
end
property_type(property) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 120
def property_type(property)
  exis = @schema.find("/p:properties/*[name()='#{property}']|/p:properties/p:optional/*[name()='#{property}']")
  exis.any? ? exis.first.attributes['type'].to_sym : nil
end
valid_state?(property,current,new) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 111
def valid_state?(property,current,new)
  @schema.find("boolean(/p:properties/p:#{property}/p:#{current}/p:#{new}[@putable='true'])") || schema.find("boolean(/p:properties/p:optional/p:#{property}/p:#{current}/p:#{new}[@putable='true'])")
end

Private Instance Methods

add_schema(key,name) click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 97
def add_schema(key,name)
  raise "schema file not found" unless File.exist?(name)
  @schemas[key] = XML::Smart.open_unprotected(name.gsub(/^\/+/,'/'))
  @schemas[key].register_namespace 'p', 'http://riddl.org/ns/common-patterns/properties/1.0'
  if !File::exist?(Riddl::Utils::Properties::PROPERTIES_SCHEMA_XSL_RNG)
    raise "properties schema transformation file not found"
  end
  @rngs[key] = @schemas[key].transform_with(XML::Smart.open_unprotected(Riddl::Utils::Properties::PROPERTIES_SCHEMA_XSL_RNG))
end