Module: SimpleFeed::DSL

Included in:
Formatter
Defined in:
lib/simplefeed/dsl.rb,
lib/simplefeed/dsl/formatter.rb,
lib/simplefeed/dsl/activities.rb

Overview

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

Defined Under Namespace

Modules: Formatter Classes: Activities

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.debugObject

Returns the value of attribute debug.



24
25
26
# File 'lib/simplefeed/dsl.rb', line 24

def debug
  @debug
end

Returns the value of attribute print_method.



78
79
80
# File 'lib/simplefeed/dsl/formatter.rb', line 78

def print_method
  @print_method
end

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/simplefeed/dsl.rb', line 26

def debug?
  debug
end

Instance Method Details

#_puts(*args) ⇒ Object



85
86
87
# File 'lib/simplefeed/dsl/formatter.rb', line 85

def _puts(*args)
  send(SimpleFeed::DSL.print_method, "\n" + args.join)
end

#event(value, at = Time.now) ⇒ Object



36
37
38
# File 'lib/simplefeed/dsl.rb', line 36

def event(value, at = Time.now)
  SimpleFeed::Event.new(value, at)
end

#feed_header(feed, &block) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/simplefeed/dsl/formatter.rb', line 120

def feed_header(feed, &block)
  header title:  { top_left: " « #{feed.name.capitalize} Feed » " },
         border: :thick,
         style:  {
           fg:     :black,
           bg:     :green,
           border: { fg: :bright_black, bg: :green }
         }, &block
end

#field(label, value, _sep = '') ⇒ Object



104
105
106
# File 'lib/simplefeed/dsl/formatter.rb', line 104

def field(label, value, _sep = '')
  field_label(label) + '' + field_value(value)
end

#field_label(text) ⇒ Object



89
90
91
# File 'lib/simplefeed/dsl/formatter.rb', line 89

def field_label(text)
  sprintf ' %20s ', text
end

#field_value(value) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/simplefeed/dsl/formatter.rb', line 93

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) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/simplefeed/dsl/formatter.rb', line 130

def header(*args, **opts)
  message = args.join("\n")
  msg     = block_given? ? (yield || message) : message + "\n"
  config  = box_config(**opts)
  lines   = Array(msg).flatten
  box     = TTY::Box.frame(*lines, **config)
  output "\n#{box}"
end

#hrObject



108
109
110
# File 'lib/simplefeed/dsl/formatter.rb', line 108

def hr
  output hr_string.magenta
end

#hr_stringObject



112
113
114
# File 'lib/simplefeed/dsl/formatter.rb', line 112

def hr_string
  '' * width + "\n"
end

#output(*args, **opts, &block) ⇒ Object



81
82
83
# File 'lib/simplefeed/dsl/formatter.rb', line 81

def output(*args, **opts, &block)
  send(SimpleFeed::DSL.print_method, *args, **opts, &block)
end

#widthObject



116
117
118
# File 'lib/simplefeed/dsl/formatter.rb', line 116

def width
  @width ||= [[TTY::Screen.width - 5, 60].max, 75].min
end

#with_activity(activity, **opts, &block) ⇒ Object



31
32
33
34
# 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