Sha256: 6178699137c7a4fc359b2d972a59927b9127ad35d91e71e5fe2edbb836c660df

Contents?: true

Size: 980 Bytes

Versions: 1

Compression:

Stored size: 980 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
      find_or_build_attribute(path, child) unless path.empty?

      child
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
horizon_client-0.2.2 lib/horizon_client/entity.rb