Class: Styler::Collection
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Styler::Collection
- Defined in:
- lib/styler/collection.rb
Instance Attribute Summary collapse
-
#collection_names ⇒ Object
readonly
Returns the value of attribute collection_names.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#style_names ⇒ Object
readonly
Returns the value of attribute style_names.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #collection(name, &block) ⇒ Object
- #collection_alias(name, collection = nil, &block) ⇒ Object
- #collections ⇒ Object
- #copy_styles_from(collection:) ⇒ Object
-
#initialize(parent = nil, name = :default) ⇒ Collection
constructor
A new instance of Collection.
- #nested_collections ⇒ Object
- #nested_styles ⇒ Object
- #path ⇒ Object
- #repeted_styles ⇒ Object
- #style(name, styles = nil, &block) ⇒ Object
- #styles ⇒ Object
Constructor Details
#initialize(parent = nil, name = :default) ⇒ Collection
Returns a new instance of Collection.
4 5 6 7 8 9 10 |
# File 'lib/styler/collection.rb', line 4 def initialize(parent = nil, name = :default) super(parent) if parent @parent = parent @name = name @style_names = [] @collection_names = [] end |
Instance Attribute Details
#collection_names ⇒ Object (readonly)
Returns the value of attribute collection_names.
2 3 4 |
# File 'lib/styler/collection.rb', line 2 def collection_names @collection_names end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
2 3 4 |
# File 'lib/styler/collection.rb', line 2 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
2 3 4 |
# File 'lib/styler/collection.rb', line 2 def parent @parent end |
#style_names ⇒ Object (readonly)
Returns the value of attribute style_names.
2 3 4 |
# File 'lib/styler/collection.rb', line 2 def style_names @style_names end |
Instance Method Details
#==(other) ⇒ Object
12 13 14 |
# File 'lib/styler/collection.rb', line 12 def ==(other) self.class == other.class && name == other.name end |
#collection(name, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/styler/collection.rb', line 31 def collection(name, &block) @collection_names << name define_singleton_method(name) do |*args| self.class.new(self, name).tap do |c| c.instance_exec(*args, &block) end end end |
#collection_alias(name, collection = nil, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/styler/collection.rb', line 41 def collection_alias(name, collection = nil, &block) @collection_names << name define_singleton_method(name) do |*args| if collection collection else instance_exec(*args, &block) end end end |
#collections ⇒ Object
76 77 78 |
# File 'lib/styler/collection.rb', line 76 def collections collection_names.map { |name| send(name) } end |
#copy_styles_from(collection:) ⇒ Object
53 54 55 56 57 |
# File 'lib/styler/collection.rb', line 53 def copy_styles_from(collection:) collection.styles.each do |style| style(style.name, style.to_a) end end |
#nested_collections ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/styler/collection.rb', line 84 def nested_collections if collections.empty? [] else collections + collections.flat_map(&:nested_collections) end end |
#nested_styles ⇒ Object
80 81 82 |
# File 'lib/styler/collection.rb', line 80 def nested_styles styles + nested_collections.flat_map(&:styles) end |
#path ⇒ Object
59 60 61 62 |
# File 'lib/styler/collection.rb', line 59 def path return "root" if parent.nil? "#{parent.path}.#{name}" end |
#repeted_styles ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/styler/collection.rb', line 64 def repeted_styles nested_styles .group_by(&:to_a) .select { |_, styles| styles.count > 1 } .to_h .values end |
#style(name, styles = nil, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/styler/collection.rb', line 16 def style(name, styles = nil, &block) @style_names << name if styles define_singleton_method(name) do |*args| Styler::Style.new(self, name, styles) end else define_singleton_method(name) do |*args| styles = instance_exec(*args, &block) Styler::Style.new(self, name, styles) end end end |
#styles ⇒ Object
72 73 74 |
# File 'lib/styler/collection.rb', line 72 def styles style_names.map { |name| send(name) } end |