Class: Sqreen::BindingAccessor

Inherits:
Object
  • Object
show all
Includes:
Transforms
Defined in:
lib/sqreen/binding_accessor.rb,
lib/sqreen/binding_accessor/path_elem.rb,
lib/sqreen/binding_accessor/transforms.rb

Overview

the value located at the given binding

Defined Under Namespace

Modules: Transforms Classes: PathElem

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transforms

#concat_keys_and_values, #flat_keys, #flat_values

Constructor Details

#initialize(expression, convert = false) ⇒ BindingAccessor

Expression to be accessed

Parameters:

  • expression (String)

    expression to read

  • convert (Boolean) (defaults to: false)

    wheter to convert objects to simpler types (Array, Hash, String…)



21
22
23
24
25
26
27
28
# File 'lib/sqreen/binding_accessor.rb', line 21

def initialize(expression, convert = false)
  @transform_args = []
  @final_transform = nil
  @expression = expression
  @path = []
  @convert = convert
  parse(expression)
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



15
16
17
# File 'lib/sqreen/binding_accessor.rb', line 15

def expression
  @expression
end

#final_transformObject

Returns the value of attribute final_transform.



15
16
17
# File 'lib/sqreen/binding_accessor.rb', line 15

def final_transform
  @final_transform
end

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/sqreen/binding_accessor.rb', line 15

def path
  @path
end

#transform_argsObject (readonly)

Returns the value of attribute transform_args.



15
16
17
# File 'lib/sqreen/binding_accessor.rb', line 15

def transform_args
  @transform_args
end

Instance Method Details

#access(binding, framework = nil, instance = nil, arguments = nil, cbdata = nil, last_return = nil) ⇒ Object

Access data from the expression



31
32
33
34
35
36
37
38
# File 'lib/sqreen/binding_accessor.rb', line 31

def access(binding, framework = nil, instance = nil, arguments = nil, cbdata = nil, last_return = nil)
  env = [framework, instance, arguments, cbdata, last_return]
  value = nil
  @path.each do |component|
    value = resolve_component(value, component, binding, env)
  end
  value
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/sqreen/binding_accessor.rb', line 53

def eql?(other)
  self.class == other.class &&
    expression == other.expression &&
    @convert == other.instance_variable_get('@convert')
end

#hashObject

implement eql? and hash for uniq



49
50
51
# File 'lib/sqreen/binding_accessor.rb', line 49

def hash
  expression.hash ^ @convert.hash
end

#resolve(*args) ⇒ Object

access and transform expression for the given binding



41
42
43
44
45
46
# File 'lib/sqreen/binding_accessor.rb', line 41

def resolve(*args)
  value = access(*args)
  value = transform(value) if @final_transform
  return convert(value) if @convert
  value
end