Module: Zena::Use::ZafuSafeDefinitions::ViewMethods

Includes:
RubyLess
Defined in:
lib/zena/use/zafu_safe_definitions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_procObject

Dynamic resolution of first



48
49
50
51
52
53
54
55
56
57
# File 'lib/zena/use/zafu_safe_definitions.rb', line 48

def self.first_proc
  @@first_proc ||= Proc.new do |receiver, method|
    if elem = receiver.opts[:elem] || receiver.klass.first
      RubyLess::TypedString.new("#{receiver.raw}.first", :class => elem, :nil => true)
    else
      # should never happen
      raise RubyLess::NoMethodError.new(receiver.raw, receiver.klass, ['first'])
    end
  end
end

.join_procObject

Dynamic resolution of join



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zena/use/zafu_safe_definitions.rb', line 60

def self.join_proc
  Proc.new do |receiver, join_arg|
    # opts[:elem] = Resolution on Array or static %w{x y z}
    # TODO remove with code in RubyLessProcessing
    if elem = receiver.opts[:elem] || receiver.klass.first
      if type = RubyLess::safe_method_type_for(elem, ['to_s'])
        if type[:method] == 'to_s'
          # ok
          res = receiver.raw
        elsif type[:method] =~ /\A\w+\Z/
          res = "#{receiver.raw}.map(&#{type[:method].inspect}).compact"
        else
          res = "#{receiver.raw}.map{|_map_obj| _map_obj.#{type[:method]}}.compact"
        end
        RubyLess::TypedString.new("#{res}.join(#{join_arg.inspect})", :class => String)
      else
        raise RubyLess::NoMethodError.new(receiver.raw, receiver.klass, ['to_s'])
      end
    else
      # internal bug: we should have :elem set whenever we use Array
      raise RubyLess::NoMethodError.new(receiver.raw, receiver.klass, ['join', join_arg])
    end
  end
end

.kind_of_procObject

Dynamic resolution of kind_of



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/zena/use/zafu_safe_definitions.rb', line 13

def self.kind_of_proc
  @@kind_of_proc ||= Proc.new do |receiver, role_or_vclass|
    if role_or_vclass.kind_of?(VirtualClass)
      res = "#{receiver}.kpath_match?('#{role_or_vclass.kpath}')"
    else
      # Role
      res = "#{receiver}.has_role?(#{role_or_vclass.id})"
    end

    RubyLess::TypedString.new(res, :class => Boolean)
  end
end

.map_procObject

Dynamic resolution of map



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zena/use/zafu_safe_definitions.rb', line 27

def self.map_proc
  @@map_proc ||= Proc.new do |receiver, method|
    if elem = receiver.opts[:elem] || receiver.klass.first
      if type = RubyLess::safe_method_type_for(elem, [method.to_s])
        if type[:method] =~ /\A\w+\Z/
          res = "#{receiver.raw}.map(&#{type[:method].to_sym.inspect}).compact"
        else
          res = "#{receiver.raw}.map{|_map_obj| _map_obj.#{type[:method]}}.compact"
        end
        res = RubyLess::TypedString.new(res, :class => [type[:class]])
      else
        raise RubyLess::NoMethodError.new(receiver.raw, receiver.klass, ['map', method])
      end
    else
      # should never happen
      raise RubyLess::NoMethodError.new(receiver.raw, receiver.klass, ['map', method])
    end
  end
end

Instance Method Details

#zafu_max(a, b) ⇒ Object

Returns the largest of two values.



149
150
151
# File 'lib/zena/use/zafu_safe_definitions.rb', line 149

def zafu_max(a, b)
  [a, b].max
end

#zafu_min(a, b) ⇒ Object

Returns the smallest of two values.



144
145
146
# File 'lib/zena/use/zafu_safe_definitions.rb', line 144

def zafu_min(a, b)
  [a, b].min
end