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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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.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)

{{{



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

def data
  @data
end

#rngObject (readonly)

{{{



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

def rng
  @rng
end

#schemaObject (readonly)

{{{



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

def schema
  @schema
end

Instance Method Details

#activate_schema(name) ⇒ Object



87
88
89
90
91
92
93
94
95
# 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) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# 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) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# 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) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# 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) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Object



120
121
122
123
# 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) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# 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