Class: Foraneus
- 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
- .accessors ⇒ Object
-
.boolean(name, *args) ⇒ Object
Declares a boolean field.
- .create_instance ⇒ Object
-
.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.
-
.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.
18 19 20 |
# File 'lib/foraneus.rb', line 18 def initialize @_ = {} end |
Class Method Details
.accessors ⇒ Object
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.
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_instance ⇒ Object
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.
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.
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.
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 |
.fields ⇒ Hash<String, Converter>
Map of fields and their corresponding converters.
98 99 100 |
# File 'lib/foraneus.rb', line 98 def self.fields @fields ||= {} end |
.float(name, *args) ⇒ Object
Declares a float field.
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.
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.
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.
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.
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.
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, ...
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.
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.
197 198 199 |
# File 'lib/foraneus.rb', line 197 def valid? @errors.empty? end |