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

Constant Summary collapse

TIME_FORMAT =
'%Y-%m-%d %H:%M:%S.%L'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.debugObject

Returns the value of attribute debug.



22
23
24
# File 'lib/simplefeed/dsl.rb', line 22

def debug
  @debug
end

Returns the value of attribute print_method.



70
71
72
# File 'lib/simplefeed/dsl/formatter.rb', line 70

def print_method
  @print_method
end

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  self.debug
end

Instance Method Details

#_hr(char = '—') ⇒ Object



106
107
108
# File 'lib/simplefeed/dsl/formatter.rb', line 106

def _hr(char = '')
  char * 75 + "\n"
end

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



73
74
75
# File 'lib/simplefeed/dsl/formatter.rb', line 73

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

#_puts(*args) ⇒ Object



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

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

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



34
35
36
# File 'lib/simplefeed/dsl.rb', line 34

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

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



98
99
100
# File 'lib/simplefeed/dsl/formatter.rb', line 98

def field(label, value, sep = '')
  _print field_label(label).italic + field_value(value).cyan.bold + sep
end

#field_label(text) ⇒ Object



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

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

#field_value(value) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/simplefeed/dsl/formatter.rb', line 87

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(message = nil) ⇒ Object



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

def header(message = nil)
  _print(_hr.green.bold)
  block_given? ? yield : _print(message.green.italic + "\n")
  _print(_hr.green.bold)
end

#hr(char = '—') ⇒ Object



102
103
104
# File 'lib/simplefeed/dsl/formatter.rb', line 102

def hr(char = '')
  _print(_hr(char).magenta)
end

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



29
30
31
32
# File 'lib/simplefeed/dsl.rb', line 29

def with_activity(activity, **opts, &block)
  opts.merge!({ context: self }) unless opts && opts[:context]
  SimpleFeed::DSL::Activities.new(activity, **opts).instance_eval(&block)
end