Class: DStruct::DStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/d_struct/d_struct.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes_hash) ⇒ DStruct

Returns a new instance of DStruct.



75
76
77
78
79
80
81
82
83
84
85
86
# 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

Instance Attribute Details

#to_hObject (readonly)

Returns the value of attribute to_h.



6
7
8
# File 'lib/d_struct/d_struct.rb', line 6

def to_h
  @to_h
end

Class Method Details

.attributes(attributes_hash) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

Instance Method Details

#add_validation_schema(*schema) ⇒ Object



71
72
73
# File 'lib/d_struct/d_struct.rb', line 71

def add_validation_schema(*schema)
  @validation_schemas << schema
end

#errorsObject



88
89
90
91
92
# 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?Boolean

call once, it is cached in @errors

Returns:

  • (Boolean)


95
96
97
# File 'lib/d_struct/d_struct.rb', line 95

def valid?
  errors.empty?
end