Sha256: 682ef3431684132a1fbebdb1c6d24e80d5c40c179058812fddb074fb4ff4f64b

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module Hive
  
  # Tracks state.
  class State < Base
    cattr_reader :methods
    self.table_name = :hive_state
    
    @@methods = %i(
      block_num db_version steem_per_mvest usd_per_steem sbd_per_steem dgpo
    )
    
    def self.respond_to_missing?(m, include_private = false)
      methods.include? m.to_sym
    end

    def self.method_missing(m, *args, &block)
      super unless respond_to_missing?(m)
      
      case m
      when :dgpo
        dgpo = JSON[last.send :dgpo]
        
        dgpo.each do |k, v|
          case v
          when Hash
            dgpo[k] = Struct.new(*v.keys.map(&:to_sym)).new(*v.values)
          when String
            if v =~ /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?$/
              dgpo[k] = Time.parse(v + 'Z') rescue v
            elsif v =~ /[0-9]+/
              dgpo[k] = v.to_i
            end
          end
        end
        
        dgpo = Struct.new(*dgpo.keys.map(&:to_sym)).new(*dgpo.values)
      else; last.send(m)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hivemind-ruby-0.1.0 lib/hive/models/state.rb