Class: Linkage::MetaObject

Inherits:
Object
  • Object
show all
Defined in:
lib/linkage/meta_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, side = nil) ⇒ MetaObject

Creates a new MetaObject.

Parameters:

  • object (Object)

    This can be a Field, Function or a regular Ruby object (Fixnum, String, etc). If ‘object` is not static (a Field or a Function that contains one or more Field objects), you should specify which “side” of the linkage the object belongs to (left-hand side or right-hand side) in the `side` argument.

  • side (Symbol) (defaults to: nil)

    ‘:lhs` for left-hand side or `:rhs` for right-hand side



15
16
17
18
19
20
21
22
# File 'lib/linkage/meta_object.rb', line 15

def initialize(object, side = nil)
  @object = object
  @static = object.kind_of?(Linkage::Data) ? object.static? : true
  if !side.nil? && side != :lhs && side != :rhs
    raise ArgumentError, "invalid `side` argument, must be :lhs or :rhs"
  end
  @side = side
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/linkage/meta_object.rb', line 3

def object
  @object
end

#sideObject



24
25
26
27
28
29
# File 'lib/linkage/meta_object.rb', line 24

def side
  if !@static && @side.nil?
    raise RuntimeError, "Object is dynamic and side is not set"
  end
  @side
end

Instance Method Details

#collationSymbol

Returns the collation of the underlying object.

Returns:

  • (Symbol)


126
127
128
129
130
131
132
# File 'lib/linkage/meta_object.rb', line 126

def collation
  if @object.kind_of?(Linkage::Data)
    @object.collation
  else
    nil
  end
end

#database_typeObject



43
44
45
46
# File 'lib/linkage/meta_object.rb', line 43

def database_type
  ds = dataset
  ds ? ds.database_type : nil
end

#datasetObject



31
32
33
# File 'lib/linkage/meta_object.rb', line 31

def dataset
  @object.kind_of?(Linkage::Data) ? @object.dataset : nil
end

#dataset=(dataset) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/linkage/meta_object.rb', line 35

def dataset=(dataset)
  if @object.kind_of?(Linkage::Data)
    @object.dataset = dataset
  else
    raise RuntimeError, "You can't set the dataset of a non-data object."
  end
end

#datasets_equal?(other) ⇒ Boolean

Returns true if the argument has the same dataset as the instance.

Parameters:

Returns:

  • (Boolean)


64
65
66
# File 'lib/linkage/meta_object.rb', line 64

def datasets_equal?(other)
  other.is_a?(Linkage::MetaObject) && other.dataset == self.dataset
end

#merge(other) ⇒ Linkage::MergeField

Returns a Linkage::MergeField if both objects are Data objects, otherwise, raises an exception.

Returns:



102
103
104
105
106
107
108
# File 'lib/linkage/meta_object.rb', line 102

def merge(other)
  if @object.kind_of?(Linkage::Data) && other.object.kind_of?(Linkage::Data)
    @object.merge(other.object)
  else
    raise ArgumentError, "Cannot merge a non-data object"
  end
end

#nameSymbol?

Return the name of the object for Data objects, nil for others.

Returns:

  • (Symbol, nil)


90
91
92
93
94
95
96
# File 'lib/linkage/meta_object.rb', line 90

def name
  if @object.kind_of?(Linkage::Data)
    @object.name
  else
    nil
  end
end

#objects_equal?(other) ⇒ Boolean

Returns true if the argument has the same object as the instance.

Parameters:

Returns:

  • (Boolean)


56
57
58
# File 'lib/linkage/meta_object.rb', line 56

def objects_equal?(other)
  other.is_a?(Linkage::MetaObject) && other.object == self.object
end

#raw?Boolean

Returns true if underlying object is not a subclass of Data.

Returns:

  • (Boolean)


135
136
137
# File 'lib/linkage/meta_object.rb', line 135

def raw?
  !@object.kind_of?(Linkage::Data)
end

#ruby_typeHash

Returns the Ruby type of the underlying object.

Returns:

  • (Hash)

See Also:



115
116
117
118
119
120
121
# File 'lib/linkage/meta_object.rb', line 115

def ruby_type
  if @object.kind_of?(Linkage::Data)
    @object.ruby_type
  else
    {:type => @object.class}
  end
end

#static?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/linkage/meta_object.rb', line 48

def static?
  @static
end

#to_exprObject

Returns an expression suitable for use in Sequel queries.

Returns:

  • (Object)


70
71
72
73
74
75
76
# File 'lib/linkage/meta_object.rb', line 70

def to_expr
  if @object.kind_of?(Linkage::Data)
    @object.to_expr
  else
    @object
  end
end

#to_identifierSequel::SQL::Identifier, Object

Returns a Sequel identifier for Data objects, or the object itself.

Returns:

  • (Sequel::SQL::Identifier, Object)


80
81
82
83
84
85
86
# File 'lib/linkage/meta_object.rb', line 80

def to_identifier
  if @object.kind_of?(Linkage::Data)
    Sequel::SQL::Identifier.new(@object.to_expr)
  else
    @object
  end
end