class Palantir::Base
Attributes
data[RW]
default[RW]
Public Class Methods
new(data, default = 0)
click to toggle source
Create a Palantir
.
Example:
>> p = Palantir::Base.new([{key: 'key', value: 'value'}]) => #<Palantir::Base:0x007fe1de9642f0 @data=[{:foo=>"bar"}], @default=0>
Arguments:
data: (Array) default: (Integer)
# File lib/palantir/base.rb, line 15 def initialize(data, default = 0) @data = data @default = default end
Public Instance Methods
size()
click to toggle source
Return the @data size in the Palantir
instance.
# File lib/palantir/base.rb, line 22 def size @data.count end
sum(key, first_value = 0)
click to toggle source
Sum data of a specific key, if any, otherwise add the default_value. This ends a chain of 'where'.
Example:
>> palantir.where(:class, 'wizard').sum(:age)
Arguments:
key: (Symbol) first_value: (Integer)
# File lib/palantir/base.rb, line 52 def sum(key, first_value = 0) @data.reduce(first_value) do |sum, element| sum += element.fetch(key){ @default } end end
where(key, value)
click to toggle source
Filter data with a where clause given a pair <key, value>. Chainable
Example:
>> palantir.where(:class, 'wizard').where(:name, 'Gandalf')
Arguments:
key: (Symbol) value: (String, Integer, Decimal...)
# File lib/palantir/base.rb, line 35 def where(key, value) filtered_data = @data.select do |element| element.has_key?(key) && element.fetch(key) == value end self.class.new(filtered_data, @default) end