Class: Foraneus

Inherits:
Object
  • Object
show all
Defined in:
lib/foraneus.rb,
lib/foraneus/errors.rb,
lib/foraneus/converters/date.rb,
lib/foraneus/converters/noop.rb,
lib/foraneus/converters/float.rb,
lib/foraneus/converters/string.rb,
lib/foraneus/converters/boolean.rb,
lib/foraneus/converters/decimal.rb,
lib/foraneus/converters/integer.rb

Overview

Foraneus base class used to declare a data set, aka ‘form’.

Defined Under Namespace

Modules: Converters Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeForaneus

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Foraneus.



18
19
20
21
22
23
# File 'lib/foraneus.rb', line 18

def initialize
  @data = {}
  @raw_data = {}

  @errors = {}
end

Instance Attribute Details

#dataHash

Returns Parsed data.

Returns:

  • (Hash)

    Parsed data.



15
16
17
# File 'lib/foraneus.rb', line 15

def data
  @data
end

Class Method Details

.boolean(name, *args) ⇒ Object

Declares a boolean field.

Parameters:

  • name (Symbol)

    The name of the field.



28
29
30
31
# File 'lib/foraneus.rb', line 28

def self.boolean(name, *args)
  converter = Foraneus::Converters::Boolean.new(*args)
  field(name, converter)
end

.date(name, *args) ⇒ Object

Declares a date field.

Parameters:

  • name (Symbol)

    The name of the field.

  • opts (Hash)


37
38
39
40
# File 'lib/foraneus.rb', line 37

def self.date(name, *args)
  converter = Foraneus::Converters::Date.new(*args)
  field(name, converter)
end

.decimal(name, *args) ⇒ Object

Declares a decimal field.

Parameters:

  • name (Symbol)

    The name of the field.



46
47
48
49
# File 'lib/foraneus.rb', line 46

def self.decimal(name, *args)
  converter = Foraneus::Converters::Decimal.new(*args)
  field(name, converter)
end

.field(name, converter = nil) ⇒ Object

Declares a field.

When no converter is given, noop is assigned.

Parameters:

  • name (Symbol)

    The name of the field.

  • converter (#parse, #raw) (defaults to: nil)

    The converter.



91
92
93
94
95
96
# File 'lib/foraneus.rb', line 91

def self.field(name, converter = nil)
  converter ||= Foraneus::Converters::Noop.new

  fields[name.to_s] = converter
  self.send(:attr_accessor, name)
end

.fieldsHash<String, Converter>

Map of fields and their corresponding converters.

Returns:

  • (Hash<String, Converter>)


101
102
103
# File 'lib/foraneus.rb', line 101

def self.fields
  @fields ||= {}
end

.float(name, *args) ⇒ Object

Declares a float field.

Parameters:

  • name (Symbol)

    The name of the field.

  • opts (Hash)


55
56
57
58
# File 'lib/foraneus.rb', line 55

def self.float(name, *args)
  converter = Foraneus::Converters::Float.new(*args)
  field(name, converter)
end

.integer(name, *args) ⇒ Object

Declares an integer field.

Parameters:

  • name (Symbol)

    The name of the field.

  • opts (Hash)


64
65
66
67
# File 'lib/foraneus.rb', line 64

def self.integer(name, *args)
  converter = Foraneus::Converters::Integer.new(*args)
  field(name, converter)
end

.noop(name, *args) ⇒ Object

Declares a noop field.

Parameters:

  • name (Symbol)

    The name of the field.



72
73
74
75
# File 'lib/foraneus.rb', line 72

def self.noop(name, *args)
  converter = Foraneus::Converters::Noop.new(*args)
  field(name, converter)
end

.parse(data = {}) ⇒ Foraneus

Parses data coming from an external source.

Parameters:

  • data (Hash<Symbol, String>) (defaults to: {})

    External data.

Returns:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/foraneus.rb', line 110

def self.parse(data = {})
  instance = self.new

  parsed_keys = []

  fields.each do |field, converter|
    given_key = field
    v = data.fetch(given_key) do
      given_key = field.to_sym
      data.fetch(given_key, nil)
    end

    parsed_keys << given_key
    __parse_raw_datum(given_key, v, instance, converter)
  end

  data.each do |k, v|
    unless parsed_keys.include?(k)
      instance[k] = v
    end
  end

  instance
end

.raw(data = {}) ⇒ Foraneus

Converts data into an external representation.

Parameters:

  • data (Hash<Symbol, Object>) (defaults to: {})

Returns:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/foraneus.rb', line 140

def self.raw(data = {})
  instance = self.new

  fields.each do |field, converter|
    given_key = field

    v = data.fetch(given_key) do
      given_key = field.to_sym
      data.fetch(given_key, nil)
    end

    instance.send("#{field}=", v)

    if v.nil?
      v = converter.opts[:default]
    end

    s = if v.nil?
      nil
    else
      converter.raw(v)
    end

    instance[given_key] = s
    instance.data[given_key] = v
  end

  instance
end

.string(name, *args) ⇒ Object

Declares a string field.

Parameters:

  • name (Symbol)

    The name of the field.



80
81
82
83
# File 'lib/foraneus.rb', line 80

def self.string(name, *args)
  converter = Foraneus::Converters::String.new(*args)
  field(name, converter)
end

Instance Method Details

#[](m = nil) ⇒ Hash, ...

Returns:

  • (Hash)

    raw data when m == nil.

  • (Array<Error>)

    errors when m == :errors.

  • (String)

    raw data value for the field m.



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/foraneus.rb', line 173

def [](m = nil)
  if m == :errors
    @errors
  elsif m.nil?
    @raw_data
  else
    @raw_data.fetch(m) do
      @raw_data[m.to_s]
    end
  end
end

#[]=(k, v) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets a raw value.

Parameters:

  • k (Symbol)

    Field name.

  • v (String)

    Raw value.



191
192
193
# File 'lib/foraneus.rb', line 191

def []=(k, v)
  @raw_data[k] = v
end

#valid?Boolean

Returns true if no conversion errors occurred. false otherwise.

Returns:

  • (Boolean)


196
197
198
# File 'lib/foraneus.rb', line 196

def valid?
  @errors.empty?
end