Class: Versionomy::Schema::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/versionomy/schema/wrapper.rb

Overview

Schemas are generally referenced through an object of this class.

Instance Method Summary collapse

Constructor Details

#initialize(field_, modules_ = [], aliases_ = {}) ⇒ Wrapper

Create a new schema wrapper object given a root field. This is a low-level method. Usually you should call Versionomy::Schema#create instead.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/versionomy/schema/wrapper.rb', line 75

def initialize(field_, modules_=[], aliases_={})
  @root_field = field_
  @names = @root_field._descendants_by_name
  @modules = modules_
  @aliases = {}
  aliases_.each do |k_,v_|
    k_ = k_.to_sym
    v_ = v_.to_sym
    if @names.include?(v_) && !@names.include?(k_)
      @aliases[k_] = v_
    end
  end
end

Instance Method Details

#==(obj_) ⇒ Object

Returns true if this schema is compatible with the other schema. Two schemas are compatible if their root fields are the same– which means that the entire field tree is the same. They may, however, include different value modules. Note that this is different from the definition of eql?.



117
118
119
# File 'lib/versionomy/schema/wrapper.rb', line 117

def ==(obj_)
  eql?(obj_)
end

#===(obj_) ⇒ Object

If the RHS is a schema, returns true if the schemas are equivalent. If the RHS is a value, returns true if the value uses this schema.



125
126
127
128
129
130
131
# File 'lib/versionomy/schema/wrapper.rb', line 125

def ===(obj_)
  if obj_.kind_of?(Value)
    obj_.schema == self
  else
    obj_ == self
  end
end

#aliasesObject

Returns a hash of field name aliases.



185
186
187
# File 'lib/versionomy/schema/wrapper.rb', line 185

def aliases
  @aliases.dup
end

#canonical_name(name_) ⇒ Object

Return the canonical field name given a name, or nil if the name is not recognized.



149
150
151
152
153
# File 'lib/versionomy/schema/wrapper.rb', line 149

def canonical_name(name_)
  name_ = name_.to_sym
  name_ = @aliases[name_] || name_
  @names.include?(name_) ? name_ : nil
end

#eql?(obj_) ⇒ Boolean

Returns true if this schema is equivalent to the other schema. Two schemas are equivalent if their root fields are the same– which means that the entire field tree is the same– and they include the same value modules. Note that this is different from the definition of ==.

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/versionomy/schema/wrapper.rb', line 105

def eql?(obj_)
  return false unless obj_.kind_of?(Schema::Wrapper)
  return @root_field == obj_.root_field && @modules == obj_.modules && @aliases == obj_.aliases
end

#field_named(name_, include_aliases_ = false) ⇒ Object

Return the field with the given name, or nil if the given name is not found in this schema. If include_aliases_ is set to true, this also supports lookup by alias.



160
161
162
163
164
# File 'lib/versionomy/schema/wrapper.rb', line 160

def field_named(name_, include_aliases_=false)
  name_ = name_.to_sym
  name_ = @aliases[name_] || name_ if include_aliases_
  @names[name_]
end

#hashObject

:nodoc:



134
135
136
# File 'lib/versionomy/schema/wrapper.rb', line 134

def hash  # :nodoc:
  @hash ||= @root_field.hash ^ @modules.hash
end

#inspectObject

:nodoc:



90
91
92
# File 'lib/versionomy/schema/wrapper.rb', line 90

def inspect   # :nodoc:
  "#<#{self.class}:0x#{object_id.to_s(16)} root=#{@root_field.inspect}>"
end

#modulesObject

Returns an array of modules that should be included in values that use this schema.



178
179
180
# File 'lib/versionomy/schema/wrapper.rb', line 178

def modules
  @modules.dup
end

#namesObject

Returns an array of names present in this schema, in no particular order. Does not include aliases.



170
171
172
# File 'lib/versionomy/schema/wrapper.rb', line 170

def names
  @names.keys
end

#root_fieldObject

Returns the root (most significant) field in this schema.



141
142
143
# File 'lib/versionomy/schema/wrapper.rb', line 141

def root_field
  @root_field
end

#to_sObject

:nodoc:



94
95
96
# File 'lib/versionomy/schema/wrapper.rb', line 94

def to_s   # :nodoc:
  inspect
end