class DStruct::DStruct

Attributes

to_h[R]

Public Class Methods

attributes(attributes_hash) click to toggle source
# File lib/d_struct/d_struct.rb, line 8
def self.attributes(attributes_hash)
  @attributes_readers   = attributes_hash.values.flatten
  @string_attributes    = attributes_hash[:strings]   || []
  @integer_attributes   = attributes_hash[:integers]  || []
  @boolean_attributes   = attributes_hash[:booleans]  || []
  @array_attributes     = attributes_hash[:arrays]    || []
  @date_attributes      = attributes_hash[:dates]     || []
  @time_attributes      = attributes_hash[:times]     || []

  # generate readers
  attr_reader *@attributes_readers

  # generate writers
  @string_attributes.each do |string_attr|
    define_method "#{string_attr}=" do |str_arg|
      value = str_arg.to_s
      instance_variable_set("@#{string_attr}", value)
      @to_h[string_attr] = value
    end
  end

  @integer_attributes.each do |int_attr|
    define_method "#{int_attr}=" do |int_arg|
      value = (Integer(int_arg) rescue nil)
      instance_variable_set("@#{int_attr}", value)
      @to_h[int_attr] = value
    end
  end

  @boolean_attributes.each do |boolean_attr|
    define_method "#{boolean_attr}=" do |boolean_arg|
      value = !!boolean_arg
      value = nil if boolean_arg.nil?
      instance_variable_set("@#{boolean_attr}", value)
      @to_h[boolean_attr] = value
    end
  end

  @array_attributes.each do |arr_attr|
    define_method "#{arr_attr}=" do |arr_arg|
      value = (Array(arr_arg) rescue nil)
      instance_variable_set("@#{arr_attr}", value)
      @to_h[arr_attr] = value
    end
  end

  @date_attributes.each do |date_attr|
    define_method "#{date_attr}=" do |date_arg|
      value = (Date.parse(date_arg.to_s) rescue nil)
      instance_variable_set("@#{date_attr}", value)
      @to_h[date_attr] = value
    end
  end

  @time_attributes.each do |time_attr|
    define_method "#{time_attr}=" do |time_arg|
      value = (Time.parse(time_arg.to_s) rescue nil)
      instance_variable_set("@#{time_attr}", value)
      @to_h[time_attr] = value
    end
  end
end
new(attributes_hash) click to toggle source
# File lib/d_struct/d_struct.rb, line 75
def initialize(attributes_hash)
  @to_h = {}
  @validation_schemas = []
  attributes_hash.each do |k,v|
    begin
      send("#{k}=", v)
    rescue # unknown key
      @errors = {k => 'unknown key'}
      break
    end
  end
end

Public Instance Methods

add_validation_schema(*schema) click to toggle source
# File lib/d_struct/d_struct.rb, line 71
def add_validation_schema(*schema)
  @validation_schemas << schema
end
errors() click to toggle source
# File lib/d_struct/d_struct.rb, line 88
def errors
  return @errors if @errors               # unknown key
  return {} if @validation_schemas == []  # no schemas
  @errors ||= @validation_schemas.flatten.reduce({}){|errors, schema| errors.update(schema.call(to_h).messages)}
end
valid?() click to toggle source

call once, it is cached in @errors

# File lib/d_struct/d_struct.rb, line 95
def valid?
  errors.empty?
end