class GoldenFleece::Value

Attributes

record[R]
schema[R]
value[R]

Public Class Methods

new(schema) click to toggle source
# File lib/golden_fleece/value.rb, line 8
def initialize(schema)
  @schema = schema
end

Public Instance Methods

compute(record) click to toggle source
# File lib/golden_fleece/value.rb, line 12
def compute(record)
  @record = record
  @value = Hana::Pointer.new(schema.json_path).eval(record.read_attribute(schema.attribute))

  cast_booleans
  apply_normalizers
  apply_default

  value
end

Private Instance Methods

apply_default() click to toggle source

If there's a persisted value, use that If not, use the default value; if the default is a lambda, call it

# File lib/golden_fleece/value.rb, line 40
def apply_default
  @value = if value.nil? || schema.parent?
    if schema.parent?
      d = schema.reduce({}) { |memo, (subschema_name, subschema)|
        memo[subschema_name.to_s] = subschema.value.compute(record)
        memo
      }
      d.values.compact.empty? ? nil : d
    elsif schema.default.respond_to?(:call)
      schema.default.call(record)
    else
      schema.default
    end
  else
    value.is_a?(Hash) ? deep_stringify_keys(value) : value
  end
end
apply_normalizers() click to toggle source
# File lib/golden_fleece/value.rb, line 34
def apply_normalizers
  @value = schema.normalizers.reduce(value) { |memo, normalizer| normalizer.normalize record, memo }
end
cast_booleans() click to toggle source

Cast boolean values the way that Rails normally does on boolean columns

# File lib/golden_fleece/value.rb, line 28
def cast_booleans
  if schema.types.include? Definitions::TYPES[:boolean]
    @value = cast_boolean(value)
  end
end