Class: SyoboiCalendar::Queries::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/syoboi_calendar/queries/base.rb

Direct Known Subclasses

Channel, Program, Title

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base



54
55
56
# File 'lib/syoboi_calendar/queries/base.rb', line 54

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



52
53
54
# File 'lib/syoboi_calendar/queries/base.rb', line 52

def options
  @options
end

Class Method Details

.option(*options) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/syoboi_calendar/queries/base.rb', line 9

def option(*options)
  options.each do |option|
    define_method(option) do
      self.options[option]
    end
  end
end

.property(*properties) ⇒ Object



5
6
7
# File 'lib/syoboi_calendar/queries/base.rb', line 5

def property(*properties)
  self.properties += properties
end

.time_option(*names) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/syoboi_calendar/queries/base.rb', line 17

def time_option(*names)
  names.each do |name|
    define_method("has_#{name}_time?") do
      !!(send("#{name}_from") || send("#{name}_to"))
    end

    define_method("formatted_#{name}_from") do
      send("#{name}_from").try(:strftime, "%Y%m%d_%H%M%S")
    end

    define_method("formatted_#{name}_to") do
      send("#{name}_to").try(:strftime, "%Y%m%d_%H%M%S")
    end
  end
end

Instance Method Details

#last_updateObject



72
73
74
# File 'lib/syoboi_calendar/queries/base.rb', line 72

def last_update
  "#{formatted_updated_from}-#{formatted_updated_to}" if has_updated_time?
end

#propertiesObject



68
69
70
# File 'lib/syoboi_calendar/queries/base.rb', line 68

def properties
  self.class.properties
end

#to_hashObject



58
59
60
# File 'lib/syoboi_calendar/queries/base.rb', line 58

def to_hash
  to_hash_with_nil_value.reject {|key, value| value.nil? }
end

#to_hash_with_nil_valueObject



62
63
64
65
66
# File 'lib/syoboi_calendar/queries/base.rb', line 62

def to_hash_with_nil_value
  properties.inject({}) do |hash, property|
    hash.merge(property => send(property.to_s.underscore))
  end
end