Class: Things2THL::ThingsNode

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

Overview

Wrapper around an AS Things node to provide easier access to its props For each property ‘prop’ of the node, it defines two methods: prop() returns the value, converting :missing_value to nil prop?() returns true/false to indicate if the value is set and is not :missing_value Not all the AS properties respond properly to get(), so not all of the auto-defined methods will work properly.

Constant Summary collapse

@@defined =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ ThingsNode

Returns a new instance of ThingsNode.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/Things2THL.rb', line 199

def initialize(node)
  @node = node
  @values = {}
  return unless @@defined.empty?
  (node.properties + node.elements).each do |prop|
    next if @@defined.has_key?(prop)
    puts "Defining ThingsNode.#{prop}" if $DEBUG
    @@defined[prop]=true
    case prop
    # For area, project and some others, convert the result to a ThingsNode as well
    when 'area', 'project', 'areas', 'projects', 'parent_tag', 'delegate'
      ThingsNode.class_eval <<-EOF
      def #{prop}
        return @values['#{prop}'] if @values.has_key?('#{prop}')
        value=node.#{prop}.get
          @values['#{prop}']=case value
                             when nil, :missing_value
                               nil
                             else
                               ThingsNode.new(value)
                             end
      end
      def #{prop}?
        !!#{prop}
      end
      EOF
    when 'to_dos', 'tags'
      # For to_dos and tags, map the returned array to ThingsNode as well
      ThingsNode.class_eval <<-EOF
      def #{prop}
        return @values['#{prop}'] if @values.has_key?('#{prop}')
        value=node.#{prop}.get
          @values['#{prop}']=case value
                             when nil, :missing_value
                               nil
                             else
                               value.map { |n| ThingsNode.new(n) }
                             end
      end
      def #{prop}?
        !!#{prop}
      end
      EOF
    else
      ThingsNode.class_eval <<-EOF
      def #{prop}
        return @values['#{prop}'] if @values.has_key?('#{prop}')
        value=node.#{prop}.get
        value=nil if value==:missing_value
        @values['#{prop}']=value
      end
      def #{prop}?
        !!#{prop}
      end
      EOF
      # Some smarts for specific attributes
      if prop == 'class_'
        ThingsNode.class_eval 'alias_method :type, :class_'
      end
    end
  end
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



195
196
197
# File 'lib/Things2THL.rb', line 195

def node
  @node
end