Class: Ick::Base

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ick/base.rb

Direct Known Subclasses

Inside, Let, My, Returning, Syntax, Wrap

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.belongs_to(clazz) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ick/base.rb', line 59

def self.belongs_to clazz
  method_name = self.underscore(self.name.split('::')[-1])
  unless clazz.method_defined?(method_name)
    clazz.class_eval " 
      def #{method_name}(value=self,&proc)
        if block_given?
          #{self.name}.instance.invoke(value, &proc)
        else
          Invoker.new(value, #{self.name})
        end
      end"
  end
end

.evaluates_in_calling_environmentObject



23
24
25
26
27
28
# File 'lib/ick/base.rb', line 23

def self.evaluates_in_calling_environment
  define_method :evaluate do |value, proc|
    proc.call(value)
  end
  true
end

.evaluates_in_value_environmentObject



30
31
32
33
34
35
# File 'lib/ick/base.rb', line 30

def self.evaluates_in_value_environment
  define_method :evaluate do |value, proc|
    value.instance_eval(&proc)
  end
  true
end

.returns_resultObject



44
45
46
47
48
49
# File 'lib/ick/base.rb', line 44

def self.returns_result
  define_method :returns do |value, result|
    result
  end
  true
end

.returns_valueObject



37
38
39
40
41
42
# File 'lib/ick/base.rb', line 37

def self.returns_value
  define_method :returns do |value, result|
    value
  end
  true
end

.underscore(camel_cased_word) ⇒ Object

snarfed from Ruby On Rails



52
53
54
55
56
57
# File 'lib/ick/base.rb', line 52

def self.underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    tr("-", "_").
    downcase
end

Instance Method Details

#evaluate(value, proc) ⇒ Object



14
15
16
# File 'lib/ick/base.rb', line 14

def evaluate(value, proc)
  raise 'implemented by subclass or by calling meta-method'
end

#invoke(value = nil, &proc) ⇒ Object



18
19
20
21
# File 'lib/ick/base.rb', line 18

def invoke(value = nil, &proc)
  result = evaluate(value, proc)
  returns(value, result)
end

#returns(value, result) ⇒ Object



10
11
12
# File 'lib/ick/base.rb', line 10

def returns(value, result)
  raise 'implemented by subclass or by calling meta-method'
end