class BitcoinNode::Protocol::Payload

Public Class Methods

defaults() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 124
def defaults
  @defaults ||= {}
end
field(name, type, options = {}) click to toggle source
# File lib/bitcoin_node/protocol.rb, line 107
def field(name, type, options = {})
  define_method(name) do
    instance_fields[name]
  end

  define_method("#{name}=") do |value|
    if type === value
      instance_fields[name] = value
    else
      instance_fields[name] = type.new(*Array(value))
    end
  end

  fields[name] = type
  defaults[name] = options[:default] if options[:default]
end
field_names() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 132
def field_names
  fields.keys
end
fields() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 128
def fields
  @fields ||= {}
end
new(attributes = {}) click to toggle source
# File lib/bitcoin_node/protocol.rb, line 151
def initialize(attributes = {})
  attributes.each do |k,v|
    self.send("#{k}=", v) 
  end
  missings = self.class.field_names - attributes.keys
  missings.each do |k|
    d = self.class.defaults[k]
    self.send("#{k}=", Proc === d ? d.call : d)
  end
end
parse(payload) click to toggle source
# File lib/bitcoin_node/protocol.rb, line 136
def parse(payload)
  result = fields.inject({}) do |memo, (field_name, type)|
    custom_parse_method = "parse_#{field_name.to_s}"
    parsed, payload = if respond_to?(custom_parse_method)
      public_send(custom_parse_method, payload, memo)
    else
      type.parse(payload)
    end
    memo[field_name] = parsed
    memo
  end
  new(result)
end

Public Instance Methods

bytesize() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 169
def bytesize
  raw.bytesize
end
inspect()
Alias for: to_s
name() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 177
def name
  type.downcase 
end
raw() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 162
def raw
  @raw ||= begin
    ordered = instance_fields.values_at(*self.class.field_names)
    ordered.map(&:pack).join
  end
end
to_s() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 181
def to_s
  "#<#{type} #{@instance_fields.inspect}>"
end
Also aliased as: inspect
type() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 173
def type
  self.class.name.split('::').last
end

Private Instance Methods

instance_fields() click to toggle source
# File lib/bitcoin_node/protocol.rb, line 188
def instance_fields
  @instance_fields ||= {}
end