class Thron::Entity::Base

Constants

DATE_REGEX
TIME_REGEX

Public Class Methods

factory(args) click to toggle source
# File lib/thron/entity/base.rb, line 12
def self.factory(args)
  case args
  when Hash
    new(args)
  when Array
    args.map { |data| new(data) }
  end
end
new(hash = {}) click to toggle source
# File lib/thron/entity/base.rb, line 21
def initialize(hash = {})
  @table = {}
  hash.each do |k,v|
    k = k.to_s.snakecase.to_sym
    @table[k] = case v
                when Hash
                  self.class.new(v)
                when Array
                  if v.first.is_a?(Hash)
                    v.map { |e| self.class.new(e) }
                  else
                    v
                  end
                when TIME_REGEX
                  Time::parse(v)
                when DATE_REGEX
                  Date::parse(v)
                else
                  v
                end
    new_ostruct_member(k)
  end
end

Public Instance Methods

to_h() click to toggle source
# File lib/thron/entity/base.rb, line 45
def to_h
  self.each_pair.reduce({}) do |acc, (k,v)|
    acc[k] = fetch_value(v, :to_h); acc
  end
end
to_payload() click to toggle source
# File lib/thron/entity/base.rb, line 51
def to_payload
  self.each_pair.reduce({}) do |acc, (k,v)|
    k = k.to_s.camelize_low
    acc[k] = fetch_value(v, :to_payload); acc
  end
end

Private Instance Methods

fetch_value(value, message) click to toggle source
# File lib/thron/entity/base.rb, line 60
def fetch_value(value, message)
  case value
  when Base
    value.send(message)
  when Array
    if value.first.is_a?(Base)
      value.map { |entity| entity.send(message) }
    else
      value
    end
  when Date, Time
    value.iso8601
  else
    value
  end
end