Class: Rasti::Form

Inherits:
Object
  • Object
show all
Extended by:
MultiRequire
Includes:
Validable
Defined in:
lib/rasti/form.rb,
lib/rasti/form/errors.rb,
lib/rasti/form/version.rb,
lib/rasti/form/castable.rb,
lib/rasti/form/types/io.rb,
lib/rasti/form/validable.rb,
lib/rasti/form/formatable.rb,
lib/rasti/form/types/enum.rb,
lib/rasti/form/types/form.rb,
lib/rasti/form/types/hash.rb,
lib/rasti/form/types/time.rb,
lib/rasti/form/types/uuid.rb,
lib/rasti/form/types/array.rb,
lib/rasti/form/types/float.rb,
lib/rasti/form/types/regexp.rb,
lib/rasti/form/types/string.rb,
lib/rasti/form/types/symbol.rb,
lib/rasti/form/types/boolean.rb,
lib/rasti/form/types/integer.rb

Defined Under Namespace

Modules: Castable, Formatable, Types, Validable Classes: CastError, MultiCastError, ValidationError

Constant Summary collapse

VERSION =
'3.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Form

Returns a new instance of Form.



48
49
50
51
52
# File 'lib/rasti/form.rb', line 48

def initialize(attrs={})
  assign_attributes attrs
  set_defaults
  validate!
end

Class Method Details

.[](attributes) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rasti/form.rb', line 16

def [](attributes)
  Class.new(self) do
    attributes.each do |name, type, options={}|
      attribute name, type, options
    end

    def self.inherited(subclass)
      subclass.instance_variable_set :@attributes, attributes.dup
    end
  end
end

.attribute(name, type, options = {}) ⇒ Object



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

def attribute(name, type, options={})
  attributes[name.to_sym] = options.merge(type: type)
  attr_reader name
end

.attribute_namesObject



37
38
39
# File 'lib/rasti/form.rb', line 37

def attribute_names
  attributes.keys
end

.attributesObject



33
34
35
# File 'lib/rasti/form.rb', line 33

def attributes
  @attributes ||= {}
end

.inspectObject



44
45
46
# File 'lib/rasti/form.rb', line 44

def to_s
  "#{name || self.superclass.name}[#{attribute_names.map(&:inspect).join(', ')}]"
end

.to_sObject



41
42
43
# File 'lib/rasti/form.rb', line 41

def to_s
  "#{name || self.superclass.name}[#{attribute_names.map(&:inspect).join(', ')}]"
end

Instance Method Details

#==(other) ⇒ Object



74
75
76
# File 'lib/rasti/form.rb', line 74

def ==(other)
  other.kind_of?(self.class) && other.attributes == attributes
end

#assigned?(name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rasti/form.rb', line 70

def assigned?(name)
  assigned_attribute_names.include? name
end

#attributes(options = {}) ⇒ Object



59
60
61
62
63
64
# File 'lib/rasti/form.rb', line 59

def attributes(options={})
  attributes_filter = {only: assigned_attribute_names, except: []}.merge(options)
  (attributes_filter[:only] - attributes_filter[:except]).each_with_object({}) do |name, hash|
    hash[name] = serialize(read_attribute(name))
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rasti/form.rb', line 78

def eql?(other)
  other.instance_of?(self.class) && other.attributes == attributes
end

#hashObject



82
83
84
# File 'lib/rasti/form.rb', line 82

def hash
  [self.class, attributes].hash
end

#to_hObject



66
67
68
# File 'lib/rasti/form.rb', line 66

def to_h
  attributes
end

#to_sObject Also known as: inspect



54
55
56
# File 'lib/rasti/form.rb', line 54

def to_s
  "#<#{self.class.name || self.class.superclass.name}[#{to_h.map { |n,v| "#{n}: #{v.inspect}" }.join(', ')}]>"
end