Class: Cubism::BlockSource

Inherits:
Struct
  • Object
show all
Defined in:
lib/cubism/cubicle_store.rb

Overview

Container for cubicle block sources

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BlockSource

Returns a new instance of BlockSource.



110
111
112
113
114
115
# File 'lib/cubism/cubicle_store.rb', line 110

def initialize(*args)
  super

  @filename, @lineno = location.split(":")
  @lineno = @lineno.to_i
end

Instance Attribute Details

#locationObject

Returns the value of attribute location

Returns:

  • (Object)

    the current value of location



93
94
95
# File 'lib/cubism/cubicle_store.rb', line 93

def location
  @location
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



93
94
95
# File 'lib/cubism/cubicle_store.rb', line 93

def source
  @source
end

#variable_nameObject

Returns the value of attribute variable_name

Returns:

  • (Object)

    the current value of variable_name



93
94
95
# File 'lib/cubism/cubicle_store.rb', line 93

def variable_name
  @variable_name
end

#view_contextObject

Returns the value of attribute view_context

Returns:

  • (Object)

    the current value of view_context



93
94
95
# File 'lib/cubism/cubicle_store.rb', line 93

def view_context
  @view_context
end

Class Method Details

.find_or_create(location:, view_context:) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/cubism/cubicle_store.rb', line 100

def self.find_or_create(location:, view_context:)
  instance = new(location: location, view_context: view_context)

  Cubism.source_store.fetch(instance.digest, instance) do |instance|
    instance.parse!
  end

  instance
end

Instance Method Details

#digestObject



127
128
129
# File 'lib/cubism/cubicle_store.rb', line 127

def digest
  ActiveSupport::Digest.hexdigest("#{location}:#{File.read(@filename)}")
end

#marshal_dumpObject



131
132
133
# File 'lib/cubism/cubicle_store.rb', line 131

def marshal_dump
  to_h.except(:view_context)
end

#marshal_load(serialized_item) ⇒ Object



135
136
137
138
139
# File 'lib/cubism/cubicle_store.rb', line 135

def marshal_load(serialized_item)
  members.excluding(:view_context).each do |arg|
    send("#{arg}=", serialized_item[arg])
  end
end

#parse!Object



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

def parse!
  return if location.start_with?("inline template")

  lines = File.readlines(@filename)[@lineno - 1..]

  preprocessor = Cubism::Preprocessor.new(source: lines.join.squish, view_context: view_context)
  self.variable_name = preprocessor.block_variable_name
  self.source = preprocessor.process
end