Class: RubyFeatures::Conditions
- Inherits:
-
Object
- Object
- RubyFeatures::Conditions
- Defined in:
- lib/ruby-features/conditions.rb
Instance Method Summary collapse
- #build_constant_postfix(*asserts) ⇒ Object
-
#initialize ⇒ Conditions
constructor
A new instance of Conditions.
- #match?(asserts, normalized = true) ⇒ Boolean
- #normalize_asserts(asserts) ⇒ Object
- #push(condition_name, block) ⇒ Object
Constructor Details
#initialize ⇒ Conditions
Returns a new instance of Conditions.
3 4 5 |
# File 'lib/ruby-features/conditions.rb', line 3 def initialize @conditions = {} end |
Instance Method Details
#build_constant_postfix(*asserts) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby-features/conditions.rb', line 40 def build_constant_postfix(*asserts) asserts = merge_asserts(asserts) RubyFeatures::Utils.camelize( asserts.sort.map { |assert_type, asserts_per_type| if asserts_per_type.empty? nil else "#{assert_type}_" + asserts_per_type.sort.map { |condition_name, condition_value| "#{condition_name}_is_#{condition_value}" }.join('_and_') end }.compact.join('_and_') ) end |
#match?(asserts, normalized = true) ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ruby-features/conditions.rb', line 26 def match?(asserts, normalized = true) asserts = normalize_asserts(asserts) unless normalized asserts[:if].each do |condition_name, condition_value| return false unless value(condition_name) == condition_value end asserts[:unless].each do |condition_name, condition_value| return false if value(condition_name) == condition_value end true end |
#normalize_asserts(asserts) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ruby-features/conditions.rb', line 14 def normalize_asserts(asserts) asserts.inject({if: {}, unless: {}}) do |mem, (assert_type, asserts_per_type)| asserts_per_type = [asserts_per_type] unless asserts_per_type.kind_of?(Array) asserts_per_type.each do |assert_per_type| assert_per_type = {assert_per_type => true} unless assert_per_type.kind_of?(Hash) mem[assert_type].merge!(assert_per_type) end mem end end |
#push(condition_name, block) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/ruby-features/conditions.rb', line 7 def push(condition_name, block) condition_name = condition_name.to_sym raise NameError.new("Such condition is already defined: #{condition_name}") if @conditions.has_key?(condition_name) @conditions[condition_name] = block end |