Class: Foraneus

Inherits:
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

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
# File 'lib/foraneus.rb', line 18

def initialize
  @_ = {}
end

Class Method Details

.accessorsObject



102
103
104
105
106
107
# File 'lib/foraneus.rb', line 102

def self.accessors
  @accessors ||= {
    :data => :data,
    :errors => :errors
  }
end

.boolean(name, *args) ⇒ Object

Declares a boolean field.

Parameters:

  • name (Symbol)

    The name of the field.



25
26
27
28
# File 'lib/foraneus.rb', line 25

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

.create_instanceObject



109
110
111
112
113
114
115
116
# File 'lib/foraneus.rb', line 109

def self.create_instance
  instance = self.new

  __singleton_attr_reader(instance, :data, {})
  __singleton_attr_reader(instance, :errors, {})

  instance
end

.date(name, *args) ⇒ Object

Declares a date field.

Parameters:

  • name (Symbol)

    The name of the field.

  • opts (Hash)


34
35
36
37
# File 'lib/foraneus.rb', line 34

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.



43
44
45
46
# File 'lib/foraneus.rb', line 43

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.



88
89
90
91
92
93
# File 'lib/foraneus.rb', line 88

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>)


98
99
100
# File 'lib/foraneus.rb', line 98

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

.float(name, *args) ⇒ Object

Declares a float field.

Parameters:

  • name (Symbol)

    The name of the field.

  • opts (Hash)


52
53
54
55
# File 'lib/foraneus.rb', line 52

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)


61
62
63
64
# File 'lib/foraneus.rb', line 61

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.



69
70
71
72
# File 'lib/foraneus.rb', line 69

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:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/foraneus.rb', line 123

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

  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:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/foraneus.rb', line 153

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

  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

    __raw_datum(given_key, v, instance, converter)
  end

  instance
end

.string(name, *args) ⇒ Object

Declares a string field.

Parameters:

  • name (Symbol)

    The name of the field.



77
78
79
80
# File 'lib/foraneus.rb', line 77

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
# File 'lib/foraneus.rb', line 173

def [](m = nil)
  if m.nil?
    @_
  else
    @_.fetch(m) do
      @_[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.



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

def []=(k, v)
  #raw_data = @_

  #raw_data[k] = v
  @_[k] = v
end

#valid?Boolean

Returns true if no conversion errors occurred. false otherwise.

Returns:

  • (Boolean)


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

def valid?
  @errors.empty?
end