Class: StaticCollection::Base
- Inherits:
-
Object
- Object
- StaticCollection::Base
- Defined in:
- lib/static_collection.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .all ⇒ Object
- .count ⇒ Object
- .find_by(opts) ⇒ Object
-
.set_source(source, defaults: {}) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .where(opts) ⇒ Object
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
83 84 85 |
# File 'lib/static_collection.rb', line 83 def initialize(attributes = {}) @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
81 82 83 |
# File 'lib/static_collection.rb', line 81 def attributes @attributes end |
Class Method Details
.all ⇒ Object
72 73 74 |
# File 'lib/static_collection.rb', line 72 def self.all instance_variable_get(:@all) end |
.count ⇒ Object
76 77 78 |
# File 'lib/static_collection.rb', line 76 def self.count all.size end |
.find_by(opts) ⇒ Object
64 65 66 |
# File 'lib/static_collection.rb', line 64 def self.find_by(opts) all.find { |instance| opts.all? { |k, v| instance.send(k) == v } } end |
.set_source(source, defaults: {}) ⇒ Object
rubocop:disable Metrics/MethodLength
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/static_collection.rb', line 7 def self.set_source(source, defaults: {}) raise "Source must be an array" unless source.is_a?(Array) defaults = defaults.stringify_keys raise "Source must have at least one value" if source.count <= 0 values = source.map(&:stringify_keys).map { |s| new(deep_freeze(defaults.merge(s))) } instance_variable_set(:@all, values) all_attribute_names = values.flat_map { |v| v.attributes.keys }.uniq all_attribute_names.each do |attribute_name| # Class methods define_singleton_method(:"find_by_#{attribute_name}") do |value| ActiveSupport::Deprecation.warn( "find_by_#{attribute_name} is deprecated for StaticCollection, " \ "use find_by(#{attribute_name}: [value])", ) all.find { |instance| instance.send(attribute_name) == value } end define_singleton_method(:"find_all_by_#{attribute_name}") do |value| ActiveSupport::Deprecation.warn( "find_all_by_#{attribute_name} is deprecated for StaticCollection, " \ "use where(#{attribute_name}: [value])", ) all.select { |instance| instance.send(attribute_name) == value } end # Instance methods send(:define_method, attribute_name) { attributes[attribute_name] } send(:define_method, "#{attribute_name}?") { attributes[attribute_name] if attributes.key?(attribute_name) } end end |
.where(opts) ⇒ Object
68 69 70 |
# File 'lib/static_collection.rb', line 68 def self.where(opts) all.select { |instance| opts.all? { |k, v| instance.send(k) == v } } end |
Instance Method Details
#as_json(options = {}) ⇒ Object
87 88 89 |
# File 'lib/static_collection.rb', line 87 def as_json( = {}) attributes.as_json() end |