Module: Schema

Includes:
Assertions
Included in:
Controls::Schema::Boolean::Example, Controls::Schema::DefaultValue::Example, Controls::Schema::DefaultValue::Proc::Example, Controls::Schema::Duplicate::Example, Controls::Schema::Equivalent::Example, Controls::Schema::Example, Controls::Schema::TransientAttributes::Example, Controls::Schema::Typed::Example, Controls::Schema::Typed::Strict::Example, Controls::Schema::Validation::Example
Defined in:
lib/schema/schema.rb,
lib/schema/data_structure.rb,
lib/schema/controls/schema.rb,
lib/schema/schema/attribute.rb,
lib/schema/validation/error.rb,
lib/schema/schema/assertions.rb,
lib/schema/controls/data_structure.rb,
lib/schema/validation/nil_attributes.rb

Defined Under Namespace

Modules: Assertions, AttributeMacro, Attributes, Boolean, Controls, DataStructure, Validation Classes: Attribute, AttributeRegistry, Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/schema/schema.rb', line 4

def self.included(cls)
  cls.class_exec do
    extend AttributeMacro
    extend Attributes

    include Virtual
    virtual :transform_write

    const_set(:Boolean, Boolean)
  end
end

Instance Method Details

#==(other, attributes = nil, ignore_class: nil) ⇒ Object Also known as: eql?



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/schema/schema.rb', line 160

def ==(other, attributes=nil, ignore_class: nil)
  attributes ||= self.class.attribute_names
  attributes = Array(attributes)

  ignore_class = false if ignore_class.nil?

  if !ignore_class
    return false if self.class != other.class
  end

  attributes.each do |attribute|
    if attribute.is_a? Hash
      this_attribute, other_attribute = attribute.keys.first, attribute.values.first
    else
      this_attribute, other_attribute = attribute, attribute
    end

    return false if public_send(this_attribute) != other.public_send(other_attribute)
  end

  true
end

#===(other) ⇒ Object



184
185
186
# File 'lib/schema/schema.rb', line 184

def ===(other)
  self.eql?(other, ignore_class: true)
end

#attributesObject Also known as: to_h



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

def attributes
  transient_attributes = []
  if self.class.respond_to?(:transient_attributes)
    transient_attributes = self.class.transient_attributes
  end

  data = self.class.attributes.each_with_object({}) do |attribute, attributes|
    next if transient_attributes.include?(attribute.name)
    attributes[attribute.name] = public_send(attribute.name)
  end

  transform_write(data)

  data
end

#attributes_equal?(other, attributes = nil, print: nil) ⇒ Boolean

Returns:



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/schema/schema.rb', line 188

def attributes_equal?(other, attributes=nil, print: nil)
  attributes ||= self.class.attribute_names

  print = true if print.nil?
  print ||= false

  equal = self.eql?(other, attributes, ignore_class: true)

  if !equal && print
    attributes = Array(attributes)

    require 'pp'
    puts "self: #{self.class.name}"
    pp self.attributes.select { |k, v| attributes.include? k }
    puts "other #{other.class.name}:"
    pp other.attributes.select { |k, v| attributes.include? k }
    puts "attributes:"
    attributes.each { |a| puts a.inspect}
  end

  equal
end