Class: Riddl::Utils::Properties::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/riddl/utils/properties.rb

Overview

}}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, target, init = nil) ⇒ Backend

Returns a new instance of Backend.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby/riddl/utils/properties.rb', line 63

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.exists?(@target)
  FileUtils::cp init, @target                if init and not File.exists?(@target)

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

Instance Attribute Details

#dataObject (readonly)

{{{



61
62
63
# File 'lib/ruby/riddl/utils/properties.rb', line 61

def data
  @data
end

#rngObject (readonly)

{{{



61
62
63
# File 'lib/ruby/riddl/utils/properties.rb', line 61

def rng
  @rng
end

#schemaObject (readonly)

{{{



61
62
63
# File 'lib/ruby/riddl/utils/properties.rb', line 61

def schema
  @schema
end

Instance Method Details

#activate_schema(name) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/ruby/riddl/utils/properties.rb', line 86

def activate_schema(name)
  if @schemas[name]
    @schema = @schemas[name]
    @rng = @rngs[name]
    true
  else
    false
  end
end

#init_state?(property, new) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/ruby/riddl/utils/properties.rb', line 116

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) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/ruby/riddl/utils/properties.rb', line 113

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) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/ruby/riddl/utils/properties.rb', line 107

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) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ruby/riddl/utils/properties.rb', line 124

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) ⇒ Object



119
120
121
122
# File 'lib/ruby/riddl/utils/properties.rb', line 119

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) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/ruby/riddl/utils/properties.rb', line 110

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