Class: SOAP::Property
- Includes:
- Enumerable
- Defined in:
- lib/soap/property.rb
Overview
Property stream format:
line separator is \r?\n. 1 line per a property.
line which begins with '#' is a comment line. empty line is ignored, too.
key/value separator is ':' or '='.
'\' as escape character. but line separator cannot be escaped.
\s at the head/tail of key/value are trimmed.
'[' + key + ']' indicates property section. for example,
[aaa.bbb]
ccc = ddd
eee.fff = ggg
[]
aaa.hhh = iii
is the same as;
aaa.bbb.ccc = ddd
aaa.bbb.eee.fff = ggg
aaa.hhh = iii
Defined Under Namespace
Modules: Util
Constant Summary collapse
- KEY_REGSRC =
'([^=:\\\\]*(?:\\\\.[^=:\\\\]*)*)'
- DEF_REGSRC =
'\\s*' + KEY_REGSRC + '\\s*[=:]\\s*(.*)'
- COMMENT_REGEXP =
COMMENT_REGEXP = Regexp.new(‘^(?:#.*|)$’, nil, ‘u’) CATDEF_REGEXP = KEY_REGSRC\\s*\\">Regexp.new(“^\$”, nil, ‘u’) LINE_REGEXP = Regexp.new(“^#DEF_REGSRC$”, nil, ‘u’)
Regexp.new('^(?:#.*|)$')
- CATDEF_REGEXP =
RubyJedi: Attempt at 1.9 Compatibility // , nil, ‘u’)
Regexp.new("^\\[\\s*#{KEY_REGSRC}\\s*\\]$")
- LINE_REGEXP =
RubyJedi: Attempt at 1.9 Compatibility // , nil, ‘u’)
Regexp.new("^#{DEF_REGSRC}$")
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(value) ⇒ Object
value: an Object key is generated by property.
-
#[](name) ⇒ Object
name: a Symbol, String or an Array.
-
#[]=(name, value) ⇒ Object
name: a Symbol, String or an Array value: an Object.
-
#add_hook(name = nil, cascade = false, &hook) ⇒ Object
name: a Symbol, String or an Array; nil means hook to the root cascade: true/false; for cascading hook of sub key hook: block which will be called with 2 args, name and value.
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Property
constructor
A new instance of Property.
- #keys ⇒ Object
- #load(stream) ⇒ Object
-
#loadproperty(propname) ⇒ Object
find property from $:.
- #lock(cascade = false) ⇒ Object
- #locked? ⇒ Boolean
- #unlock(cascade = false) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ Property
Returns a new instance of Property.
62 63 64 65 66 67 |
# File 'lib/soap/property.rb', line 62 def initialize @store = Hash.new @hook = Hash.new @self_hook = Array.new @locked = false end |
Class Method Details
.load(stream) ⇒ Object
54 55 56 |
# File 'lib/soap/property.rb', line 54 def self.load(stream) new.load(stream) end |
.loadproperty(propname) ⇒ Object
58 59 60 |
# File 'lib/soap/property.rb', line 58 def self.loadproperty(propname) new.loadproperty(propname) end |
Instance Method Details
#<<(value) ⇒ Object
value: an Object key is generated by property
135 136 137 |
# File 'lib/soap/property.rb', line 135 def <<(value) self[generate_new_key] = value end |
#[](name) ⇒ Object
name: a Symbol, String or an Array
118 119 120 |
# File 'lib/soap/property.rb', line 118 def [](name) referent(name_to_a(name)) end |
#[]=(name, value) ⇒ Object
name: a Symbol, String or an Array value: an Object
124 125 126 127 128 129 130 131 |
# File 'lib/soap/property.rb', line 124 def []=(name, value) name_pair = name_to_a(name).freeze hooks = assign(name_pair, value) hooks.each do |hook| hook.call(name_pair, value) end value end |
#add_hook(name = nil, cascade = false, &hook) ⇒ Object
name: a Symbol, String or an Array; nil means hook to the root cascade: true/false; for cascading hook of sub key hook: block which will be called with 2 args, name and value
142 143 144 145 146 147 148 149 |
# File 'lib/soap/property.rb', line 142 def add_hook(name = nil, cascade = false, &hook) if name == nil or name == true or name == false cascade = name assign_self_hook(cascade, &hook) else assign_hook(name_to_a(name), cascade, &hook) end end |
#each ⇒ Object
151 152 153 154 155 |
# File 'lib/soap/property.rb', line 151 def each @store.each do |key, value| yield(key, value) end end |
#empty? ⇒ Boolean
157 158 159 |
# File 'lib/soap/property.rb', line 157 def empty? @store.empty? end |
#keys ⇒ Object
161 162 163 |
# File 'lib/soap/property.rb', line 161 def keys @store.keys end |
#load(stream) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/soap/property.rb', line 79 def load(stream) key_prefix = "" if stream.respond_to?(:each_line) # Ruby 1.9 and beyond stream = stream.each_line elsif stream.respond_to?(:lines) # RubyJedi: compatible with Ruby 1.8.6 stream = stream.lines end stream.each_with_index do |line, lineno| line.sub!(/\r?\n\z/u, '') case line when COMMENT_REGEXP next when CATDEF_REGEXP key_prefix = $1.strip when LINE_REGEXP key, value = $1.strip, $2.strip key = "#{key_prefix}.#{key}" unless key_prefix.empty? key, value = loadstr(key), loadstr(value) self[key] = value else raise TypeError.new( "property format error at line #{lineno + 1}: `#{line}'") end end self end |
#loadproperty(propname) ⇒ Object
find property from $:.
107 108 109 110 111 112 113 114 115 |
# File 'lib/soap/property.rb', line 107 def loadproperty(propname) return loadpropertyfile(propname) if File.file?(propname) $:.each do |path| if File.file?(file = File.join(path, propname)) return loadpropertyfile(file) end end nil end |
#lock(cascade = false) ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/soap/property.rb', line 169 def lock(cascade = false) if cascade each_key do |key| key.lock(cascade) end end @locked = true self end |
#locked? ⇒ Boolean
189 190 191 |
# File 'lib/soap/property.rb', line 189 def locked? @locked end |
#unlock(cascade = false) ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/soap/property.rb', line 179 def unlock(cascade = false) @locked = false if cascade each_key do |key| key.unlock(cascade) end end self end |
#values ⇒ Object
165 166 167 |
# File 'lib/soap/property.rb', line 165 def values @store.values end |