Class: Rasti::Model

Inherits:
Object
  • Object
show all
Extended by:
MultiRequire
Defined in:
lib/rasti/model.rb,
lib/rasti/model/errors.rb,
lib/rasti/model/schema.rb,
lib/rasti/model/version.rb,
lib/rasti/model/attribute.rb

Defined Under Namespace

Modules: Schema Classes: Attribute, NotAssignedAttributeError, UnexpectedAttributesError

Constant Summary collapse

VERSION =
'2.1.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



63
64
65
66
# File 'lib/rasti/model.rb', line 63

def initialize(attributes={})
  @__attributes__ = attributes
  validate_defined_attributes! attributes.keys.map(&:to_sym)
end

Class Method Details

.[](*args) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/rasti/model.rb', line 14

def [](*args)
  Class.new(self) do
    if args.count == 1 && args.first.is_a?(Hash)
      args.first.each { |name, type| attribute name, type }
    else
      args.each { |name| attribute name }
    end
  end
end

.attribute_namesObject



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

def attribute_names
  @attibute_names ||= attributes.map(&:name)
end

.attributesObject



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

def attributes
  @attributes ||= []
end

.inspectObject



43
44
45
# File 'lib/rasti/model.rb', line 43

def to_s
  "#{model_name}[#{attribute_names.join(', ')}]"
end

.model_nameObject



32
33
34
# File 'lib/rasti/model.rb', line 32

def model_name
  name || self.superclass.name
end

.to_sObject



40
41
42
# File 'lib/rasti/model.rb', line 40

def to_s
  "#{model_name}[#{attribute_names.join(', ')}]"
end

.to_schemaObject



36
37
38
# File 'lib/rasti/model.rb', line 36

def to_schema
  Schema.serialize self
end

Instance Method Details

#==(other) ⇒ Object



118
119
120
# File 'lib/rasti/model.rb', line 118

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

#cast_attributes!Object

Raises:

  • (Rasti::Types::CompoundError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rasti/model.rb', line 72

def cast_attributes!
  errors = {}

  self.class.attributes.each do |attribute|
    begin
      if assigned_attribute?(attribute.name)
        value = read_attribute attribute
        value.cast_attributes! if value.is_a? Model
      end

    rescue Rasti::Types::CompoundError => ex
      ex.errors.each do |key, messages|
        errors["#{attribute.name}.#{key}"] = messages
      end

    rescue Rasti::Types::CastError => ex
      errors[attribute.name] = [ex.message]
    end
  end

  raise Rasti::Types::CompoundError.new(errors) unless errors.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/rasti/model.rb', line 114

def eql?(other)
  instance_of?(other.class) && to_h.eql?(other.to_h)
end

#hashObject



122
123
124
# File 'lib/rasti/model.rb', line 122

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

#merge(new_attributes) ⇒ Object



68
69
70
# File 'lib/rasti/model.rb', line 68

def merge(new_attributes)
  self.class.new __attributes__.merge(new_attributes)
end

#to_h(options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rasti/model.rb', line 95

def to_h(options={})
  if options.empty?
    serialized_attributes
  else
    attributes_filter = {only: serialized_attributes.keys, except: []}.merge(options)
    (attributes_filter[:only] - attributes_filter[:except]).each_with_object({}) do |name, hash|
      hash[name] = serialized_attributes[name]
    end
  end

end

#to_sObject Also known as: inspect



107
108
109
110
111
# File 'lib/rasti/model.rb', line 107

def to_s
  cast_attributes!

  "#{self.class.model_name}[#{__cache__.map { |n,v| "#{n}: #{v.inspect}" }.join(', ')}]"
end