Sha256: caf84c9beb4625266a8840cccf616cbf48f543d0ff071c352e0125ff79002da5

Contents?: true

Size: 970 Bytes

Versions: 5

Compression:

Stored size: 970 Bytes

Contents

module HorizonClient
  class Entity
    attr_reader :node

    def initialize(node)
      @node = node
    end

    def [](name)
      attr_node = node.locate(name).first
      get_value attr_node
    end

    def []=(name, value)
      elem = find_or_build_attribute(name.split('/'), node)

      # needed in case element is in '</elem>' form
      check = elem.text

      elem.replace_text(value)
    end

    def get_collection(name)
      collection_node = find_or_build_attribute(name.split('/'), node)
      Collection.new(collection_node)
    end

    private

    def find_or_build_attribute(path, parent)
      name = path.shift
      unless child = parent.locate(name).first
        child = Ox::Element.new(name)
        parent << child
      end
      path.empty? ? child : find_or_build_attribute(path, child)
    end

    def get_value(node)
      if node.respond_to?('href')
        node.href.text
      else
        node.text
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
horizon_client-0.2.7 lib/horizon_client/entity.rb
horizon_client-0.2.6 lib/horizon_client/entity.rb
horizon_client-0.2.5 lib/horizon_client/entity.rb
horizon_client-0.2.4 lib/horizon_client/entity.rb
horizon_client-0.2.3 lib/horizon_client/entity.rb