module SimpleFeed::DSL

This module offers a convenient DSL-based approach to manipulating user feeds.

Usage:

require 'simplefeed/dsl'
include SimpleFeed::DSL

with_activity(SimpleFeed.get(:newsfeed).activity(user_id)) do
  store(value: 'hello', at: Time.now)  #=> true
  fetch                                # => [ Event, Event, ... ]
  total_count                          # => 12
  unread_count                         # => 4
end

Attributes

debug[RW]
print_method[RW]

Public Class Methods

debug?() click to toggle source
# File lib/simplefeed/dsl.rb, line 26
def debug?
  debug
end

Public Instance Methods

_puts(*args) click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 84
def _puts(*args)
  send(SimpleFeed::DSL.print_method, "\n" + args.join)
end
event(value, at = Time.now) click to toggle source
# File lib/simplefeed/dsl.rb, line 36
def event(value, at = Time.now)
  SimpleFeed::Event.new(value, at)
end
feed_header(feed, &block) click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 119
def feed_header(feed, &block)
  header title:  { top_left: " « #{feed.name.capitalize} Feed » " },
         border: :thick,
         style:  {
           fg:     :bright_red,
           border: { fg: :white }
         }, &block
end
field(label, value, _sep = '') click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 103
def field(label, value, _sep = '')
  field_label(label) + ' -> ' + field_value(value)
end
field_label(text) click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 88
def field_label(text)
  sprintf ' %20s ', text
end
field_value(value) click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 92
def field_value(value)
  case value
  when Numeric
    sprintf '%-20d', value
  when Time
    sprintf '%-30s', value.strftime(TIME_FORMAT)
  else
    sprintf '%-20s', value.to_s
  end
end
header(*args, **opts) { ||| message) : message + "\n"| ... } click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 128
def header(*args, **opts)
  message = args.join("\n")
  msg = block_given? ? (yield || message) : message + "\n"
  box = TTY::Box.frame(box_config(**opts)) { Array(msg).join("\n") }
  output "\n#{box}"
end
hr() click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 107
def hr
  output hr_string.magenta
end
hr_string() click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 111
def hr_string
  '―' * width + "\n"
end
output(*args, **opts, &block) click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 80
def output(*args, **opts, &block)
  send(SimpleFeed::DSL.print_method, *args, **opts, &block)
end
width() click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 115
def width
  @width ||= TTY::Screen.width - 5
end
with_activity(activity, **opts, &block) click to toggle source
# File lib/simplefeed/dsl.rb, line 31
def with_activity(activity, **opts, &block)
  opts.merge!({ context: self }) unless opts && opts[:context]
  SimpleFeed::DSL::Activities.new(activity, **opts).instance_eval(&block)
end

Private Instance Methods

box_config(**opts) click to toggle source
# File lib/simplefeed/dsl/formatter.rb, line 137
def box_config(**opts)
  {
    width:   width,
    align:   :left,
    padding: [0, 3],
    style:   {
      fg:     :bright_yellow,
      border: {
        fg: :bright_magenta,
      }
    }
  }.merge(opts)
end