Sha256: c1117368f9c2573b66badb942262019358fbdb8c9fd99bef5ec0870a5934007a

Contents?: true

Size: 1.05 KB

Versions: 7

Compression:

Stored size: 1.05 KB

Contents

class Gillbus
  # Gives a bunch of class methods
  # to parse gillbus responses hash and define accessors
  #
  # Warning:
  #   resulting definitions won't work properly in subclass
  #
  # Usage:
  #     class MyClass
  #       extend Gillbus::Fields
  #       field :foo, :date
  #     end
  #
  #     MyClass.parse(hash)
  module Fields

    def parser_class
      if defined? @parser_class
        @parser_class || Gillbus::Parser
      else
        Gillbus::Parser
      end
    end

    def parse(doc, instance = new, parent = nil)
      instance ||= new
      parser_class.new(
        doc: doc,
        instance: instance,
        fields: field_definitions,
        parent: parent
      ).parse
      instance
    end

    def parser(&definition)
      @parser_class = Class.new(parser_class, &definition)
    end

    private

    def field_definitions
      @fields ||= []
    end

    def field(name, type=:string, key: name.to_s.upcase, root: nil)
      field_definitions << {name: name, key: key, type: type, root: root}
      attr_accessor name
    end


  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gillbus-0.16.7 lib/gillbus/helpers/fields.rb
gillbus-0.16.6 lib/gillbus/helpers/fields.rb
gillbus-0.16.5 lib/gillbus/helpers/fields.rb
gillbus-0.16.4 lib/gillbus/helpers/fields.rb
gillbus-0.16.3 lib/gillbus/helpers/fields.rb
gillbus-0.16.2 lib/gillbus/helpers/fields.rb
gillbus-0.16.1 lib/gillbus/helpers/fields.rb