Class: Foraneus
- 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
Defined Under Namespace
Modules: Converters, Utils Classes: Error
Class Method Summary collapse
-
.accessors ⇒ Hash
Return the names of data and error accessors.
-
.boolean(name, *args) ⇒ Object
Declares a boolean field.
-
.create_instance ⇒ Foraneus
Create a new instance while setting up data and error accessors.
-
.date(name, *args) ⇒ Object
Declares a date field.
-
.decimal(name, *args) ⇒ Object
Declares a decimal field.
-
.field(name, converter = nil) ⇒ Object
Declares a field.
-
.fields ⇒ Hash<String, Converter>
Map of fields and their corresponding converters.
-
.float(name, *args) ⇒ Object
Declares a float field.
-
.form(name) { ... } ⇒ Object
Declares a nested form field.
-
.integer(name, *args) ⇒ Object
Declares an integer field.
-
.noop(name, *args) ⇒ Object
Declares a noop field.
-
.parse(data = {}) ⇒ Foraneus
Parses data coming from an external source.
-
.raw(data = {}) ⇒ Foraneus
Converts data into an external representation.
-
.string(name, *args) ⇒ Object
Declares a string field.
Instance Method Summary collapse
- #[](m = nil) ⇒ Hash, ...
-
#[]=(k, v) ⇒ Object
private
Sets a raw value.
-
#initialize ⇒ Foraneus
constructor
private
A new instance of Foraneus.
-
#valid? ⇒ Boolean
Returns true if no conversion errors occurred.
Constructor Details
#initialize ⇒ Foraneus
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
.accessors ⇒ Hash
Return the names of data and error accessors
Returned hash contains the keys :data and :errors, where values are the proper accesor names
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.
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_instance ⇒ Foraneus
Create a new instance while setting up data and error accessors
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.
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.
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.
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 |
.fields ⇒ Hash<String, Converter>
Map of fields and their corresponding converters.
113 114 115 |
# File 'lib/foraneus.rb', line 113 def self.fields @fields ||= {} end |
.float(name, *args) ⇒ Object
Declares a float field.
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
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.
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.
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.
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.
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.
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, ...
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.
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.
201 202 203 |
# File 'lib/foraneus.rb', line 201 def valid? @errors.empty? end |