Skip to content

Commit 1378c3d

Browse files
committed
Add expression support to Flipper UI
1 parent 00bb602 commit 1378c3d

73 files changed

Lines changed: 821 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/ui/basic.ru

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Flipper::UI.configure do |config|
4848
end
4949

5050
config.application_href = "https://example.com"
51+
52+
config.expressions_enabled = true
5153
end
5254

5355
# You can uncomment these to get some default data:

lib/flipper/expression.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def evaluate(context = {})
4545
end
4646
end
4747

48+
def in_words
49+
function.in_words(*args)
50+
end
51+
4852
def eql?(other)
4953
other.is_a?(self.class) && @function == other.function && @args == other.args
5054
end

lib/flipper/expression/constant.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def evaluate(_ = nil)
1616
value
1717
end
1818

19+
def in_words
20+
value
21+
end
22+
23+
def name
24+
'Constant'
25+
end
26+
1927
def eql?(other)
2028
other.is_a?(self.class) && other.value == value
2129
end

lib/flipper/expressions/all.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ class All
44
def self.call(*args)
55
args.all?
66
end
7+
8+
def self.in_words(*args)
9+
count = args.length
10+
return args[0].in_words if count == 1
11+
12+
"all #{count} conditions"
13+
end
714
end
815
end
916
end

lib/flipper/expressions/any.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ class Any
44
def self.call(*args)
55
args.any?
66
end
7+
8+
def self.in_words(*args)
9+
count = args.length
10+
return args[0].in_words if count == 1
11+
12+
"any #{count} conditions"
13+
end
714
end
815
end
916
end

lib/flipper/expressions/boolean.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class Boolean
44
def self.call(value)
55
Flipper::Typecast.to_boolean(value)
66
end
7+
8+
def self.in_words(arg)
9+
self.call(arg.value)
10+
end
711
end
812
end
913
end

lib/flipper/expressions/comparable.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ def self.operator
88
def self.call(left, right)
99
left.respond_to?(operator) && right.respond_to?(operator) && left.public_send(operator, right)
1010
end
11+
12+
def self.in_words(left, right)
13+
"#{left.in_words} is #{operator_in_words} #{right.in_words}"
14+
end
15+
16+
def self.operator_in_words
17+
raise NotImplementedError
18+
end
1119
end
1220
end
1321
end

lib/flipper/expressions/duration.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ def self.call(scalar, unit = 'second')
2323

2424
scalar * SECONDS_PER[unit]
2525
end
26+
27+
def self.in_words(*args)
28+
return "#{args.first.in_words} seconds" if args.size == 1
29+
args.map(&:in_words).join(' ')
30+
end
2631
end
2732
end
2833
end

lib/flipper/expressions/equal.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class Equal < Comparable
44
def self.operator
55
:==
66
end
7+
8+
def self.operator_in_words
9+
'equal to'
10+
end
711
end
812
end
913
end

lib/flipper/expressions/greater_than.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class GreaterThan < Comparable
44
def self.operator
55
:>
66
end
7+
8+
def self.operator_in_words
9+
'greater than'
10+
end
711
end
812
end
913
end

0 commit comments

Comments
 (0)