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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/zena/use/zafu_safe_definitions.rb', line 117

def self.first_proc
  @@first_proc ||= Proc.new do |receiver, method|
    if elem = receiver.opts[:elem] || receiver.klass.first
      # All query contexts are only opened if they are not empty.
      could_be_nil = !receiver.opts[:query]
      RubyLess::TypedString.new("#{receiver.raw}.first", :class => elem, :nil => could_be_nil, :query => receiver.opts[:query])
    else
      # should never happen
      raise RubyLess::NoMethodError.new(receiver.raw, receiver.klass, ['first'])
    end
  end
end

.join_procObject

Dynamic resolution of join



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/zena/use/zafu_safe_definitions.rb', line 131

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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zena/use/zafu_safe_definitions.rb', line 69

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/zena/use/zafu_safe_definitions.rb', line 83

def self.map_proc
  @@map_proc ||= Proc.new do |receiver, method|
    if elem = receiver.opts[:elem] || receiver.klass.first
      if code = RubyLess.translate(elem, method)
        res = "#{receiver.raw}.map{|_map_obj| _map_obj.#{code}}.compact"
        res = RubyLess::TypedString.new(res, :class => [code.klass])
      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

.map_sumObject

Dynamic resolution of sum(“working_time”)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/zena/use/zafu_safe_definitions.rb', line 100

def self.map_sum
  @@map_sum ||= Proc.new do |receiver, method|
    if elem = receiver.opts[:elem] || receiver.klass.first
      if code = RubyLess.translate(elem, method)
        res = "#{receiver.raw}.map{|_sum_obj| _sum_obj.#{code}.to_f}.reduce(:+)"
        res = RubyLess::TypedString.new(res, :class => Number)
      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

#aparamsObject



238
239
240
# File 'lib/zena/use/zafu_safe_definitions.rb', line 238

def aparams
  @aparams ||= AParamsDictionary.new(params)
end

#hparamsObject



242
243
244
# File 'lib/zena/use/zafu_safe_definitions.rb', line 242

def hparams
  @hparams ||= HParamsDictionary.new(params)
end

#zafu_max(a, b) ⇒ Object

Returns the largest of two values.



234
235
236
# File 'lib/zena/use/zafu_safe_definitions.rb', line 234

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

#zafu_min(a, b) ⇒ Object

Returns the smallest of two values.



229
230
231
# File 'lib/zena/use/zafu_safe_definitions.rb', line 229

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