Class: Flipper::Expression

Inherits:
Object
  • Object
show all
Includes:
Builder
Defined in:
lib/flipper/expression.rb,
lib/flipper/expression/builder.rb,
lib/flipper/expression/constant.rb

Defined Under Namespace

Modules: Builder Classes: Constant

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Builder

#add, #all, #all?, #any, #any?, #build, #equal, #greater_than, #greater_than_or_equal_to, #group?, #less_than, #less_than_or_equal_to, #not_equal, #percentage_of_actors, #remove

Constructor Details

#initialize(name, args = []) ⇒ Expression

Returns a new instance of Expression.



34
35
36
37
38
# File 'lib/flipper/expression.rb', line 34

def initialize(name, args = [])
  @name = name.to_s
  @function = Expressions.const_get(name)
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



32
33
34
# File 'lib/flipper/expression.rb', line 32

def args
  @args
end

#functionObject (readonly)

Returns the value of attribute function.



32
33
34
# File 'lib/flipper/expression.rb', line 32

def function
  @function
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/flipper/expression.rb', line 32

def name
  @name
end

Class Method Details

.build(object) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flipper/expression.rb', line 8

def self.build(object)
  return object if object.is_a?(self) || object.is_a?(Constant)

  case object
  when Hash
    name = object.keys.first
    args = object.values.first
    unless name
      raise ArgumentError, "#{object.inspect} cannot be converted into an expression"
    end

    new(name, Array(args).map { |o| build(o) })
  when String, Numeric, FalseClass, TrueClass
    Expression::Constant.new(object)
  when Symbol
    Expression::Constant.new(object.to_s)
  else
    raise ArgumentError, "#{object.inspect} cannot be converted into an expression"
  end
end

Instance Method Details

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

Returns:

  • (Boolean)


48
49
50
# File 'lib/flipper/expression.rb', line 48

def eql?(other)
  other.is_a?(self.class) && @function == other.function && @args == other.args
end

#evaluate(context = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/flipper/expression.rb', line 40

def evaluate(context = {})
  if call_with_context?
    function.call(*args.map {|arg| arg.evaluate(context) }, context: context)
  else
    function.call(*args.map {|arg| arg.evaluate(context) })
  end
end

#valueObject



53
54
55
56
57
# File 'lib/flipper/expression.rb', line 53

def value
  {
    name => args.map(&:value)
  }
end