Class: PaidUp::Feature

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods, ActiveModel::Model
Defined in:
lib/paid_up/feature.rb

Overview

Feature Class: Not an ActiveRecord object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



22
23
24
# File 'lib/paid_up/feature.rb', line 22

def description
  @description
end

#setting_typeObject

Returns the value of attribute setting_type.



22
23
24
# File 'lib/paid_up/feature.rb', line 22

def setting_type
  @setting_type
end

#slugObject

Returns the value of attribute slug.



22
23
24
# File 'lib/paid_up/feature.rb', line 22

def slug
  @slug
end

#titleObject

Returns the value of attribute title.



22
23
24
# File 'lib/paid_up/feature.rb', line 22

def title
  @title
end

Class Method Details

.allObject



55
56
57
# File 'lib/paid_up/feature.rb', line 55

def all
  raw.values
end

.find(**conditions) ⇒ Object



71
72
73
# File 'lib/paid_up/feature.rb', line 71

def find(**conditions)
  find_all(conditions).first
end

.find_all(**conditions) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/paid_up/feature.rb', line 59

def find_all(**conditions)
  collection = []
  all.each do |feature|
    qualifies = true
    conditions.each do |key, value|
      feature.send(key) != value && (qualifies = false)
    end
    qualifies && collection << feature
  end
  collection
end

.find_by_slug(slug) ⇒ Object



47
48
49
# File 'lib/paid_up/feature.rb', line 47

def find_by_slug(slug)
  raw[slug.to_sym]
end

.find_by_slug!(slug) ⇒ Object



51
52
53
# File 'lib/paid_up/feature.rb', line 51

def find_by_slug!(slug)
  find_by_slug(slug) || raise(:feature_not_found.l)
end

.method_missing(method_sym, *arguments, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/paid_up/feature.rb', line 75

def method_missing(method_sym, *arguments, &block)
  if method_sym.to_s =~ /^find_by_(.*)$/
    find(Regexp.last_match[1].to_sym => arguments.first)
  elsif method_sym.to_s =~ /^find_all_by_(.*)$/
    find_all(Regexp.last_match[1].to_sym => arguments.first)
  else
    super
  end
end

.rawObject



43
44
45
# File 'lib/paid_up/feature.rb', line 43

def raw
  PaidUp.features
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/paid_up/feature.rb', line 85

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?('find_') || super
end

Instance Method Details

#feature_modelObject



102
103
104
# File 'lib/paid_up/feature.rb', line 102

def feature_model
  feature_model_name.constantize
end

#feature_model_nameObject



94
95
96
97
98
99
100
# File 'lib/paid_up/feature.rb', line 94

def feature_model_name
  acceptable_setting_types = %w(table_rows rolify_rows)
  unless acceptable_setting_types.include? setting_type
    raise :no_implicit_conversion_of_type_features.l(type: setting_type)
  end
  slug.classify
end

#for_graphql(setting) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/paid_up/feature.rb', line 106

def for_graphql(setting)
  OpenStruct.new(
    id: slug,
    title: title,
    slug: slug,
    description: description,
    setting: setting,
    setting_human: setting_human(setting)
  )
end

#setting_human(setting) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/paid_up/feature.rb', line 117

def setting_human(setting)
  if setting == PaidUp::Unlimited.to_i
    PaidUp::Unlimited.to_s
  elsif setting_type == 'boolean'
    setting ? :allowed.l : :not_allowed.l
  else
    setting.to_s
  end
end

#to_sObject



90
91
92
# File 'lib/paid_up/feature.rb', line 90

def to_s
  slug
end