Class: Foraneus

Inherits:
Object show all
Defined in:
lib/foraneus.rb,
lib/foraneus/utils.rb,
lib/foraneus/errors.rb,
lib/foraneus/converters/date.rb,
lib/foraneus/converters/noop.rb,
lib/foraneus/converters/float.rb,
lib/foraneus/converters/nested.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’.

Direct Known Subclasses

Converters::Nested

Defined Under Namespace

Modules: Converters, Utils 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.



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

def initialize
  @_ = {} # Hash that holds external representation data
end

Class Method Details

.accessorsHash

Return the names of data and error accessors

Returned hash contains the keys :data and :errors, where values are the proper accesor names

Returns:

  • (Hash)


122
123
124
125
126
127
# File 'lib/foraneus.rb', line 122

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.



31
32
33
34
# File 'lib/foraneus.rb', line 31

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

.create_instanceForaneus

Create a new instance while setting up data and error accessors

Returns:



132
133
134
135
136
137
138
139
# File 'lib/foraneus.rb', line 132

def self.create_instance
  instance = self.new

  Utils.singleton_attr_accessor(instance, :data, {})
  Utils.singleton_attr_accessor(instance, :errors, {})

  instance
end

.date(name, *args) ⇒ Object

Declares a date field.

Parameters:

  • name (Symbol)

    The name of the field.

  • opts (Hash)


40
41
42
43
# File 'lib/foraneus.rb', line 40

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.



49
50
51
52
# File 'lib/foraneus.rb', line 49

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.



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

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


113
114
115
# File 'lib/foraneus.rb', line 113

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

.float(name, *args) ⇒ Object

Declares a float field.

Parameters:

  • name (Symbol)

    The name of the field.

  • opts (Hash)


58
59
60
61
# File 'lib/foraneus.rb', line 58

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

.form(name) { ... } ⇒ Object

Declares a nested form field

Parameters:

  • name (Symbol)

    The name of the field.

Yields:

  • Yields to a nested foraneus spec.



67
68
69
70
# File 'lib/foraneus.rb', line 67

def self.form(name, &block)
  converter = Class.new(Foraneus::Converters::Nested, &block)
  field(name, converter)
end

.integer(name, *args) ⇒ Object

Declares an integer field.

Parameters:

  • name (Symbol)

    The name of the field.

  • opts (Hash)


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

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.



84
85
86
87
# File 'lib/foraneus.rb', line 84

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:



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/foraneus.rb', line 146

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

  instance.instance_variable_set(:@_, data.dup)

  fields.each do |field, converter|
    __parse_field(data, field, converter, instance)
  end

  instance
end

.raw(data = {}) ⇒ Foraneus

Converts data into an external representation.

Parameters:

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

Returns:



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/foraneus.rb', line 164

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

  instance.send("#{self.accessors[:data]}=", data)

  fields.each do |field, converter|
    __raw_field(data, field, converter, instance)
  end

  instance
end

.string(name, *args) ⇒ Object

Declares a string field.

Parameters:

  • name (Symbol)

    The name of the field.



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

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.



180
181
182
183
184
185
186
187
188
# File 'lib/foraneus.rb', line 180

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



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

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

#valid?Boolean

Returns true if no conversion errors occurred. false otherwise.

Returns:

  • (Boolean)


201
202
203
# File 'lib/foraneus.rb', line 201

def valid?
  @errors.empty?
end