Class: PluginContext
- Inherits:
-
Object
- Object
- PluginContext
- Includes:
- ClassLogging, PerfStats
- Defined in:
- lib/httpthumbnailer/plugin.rb
Constant Summary collapse
- PluginArgumentError =
Class.new ArgumentError
Instance Attribute Summary collapse
-
#edits ⇒ Object
readonly
Returns the value of attribute edits.
-
#thumbnailing_methods ⇒ Object
readonly
Returns the value of attribute thumbnailing_methods.
Class Method Summary collapse
Instance Method Summary collapse
- #center_to_offset(center_x, center_y, w, h) ⇒ Object
- #edit(name, &block) ⇒ Object
- #float!(name, arg, default = nil) ⇒ Object
-
#initialize(&block) ⇒ PluginContext
constructor
A new instance of PluginContext.
-
#int!(name, arg, default = nil) ⇒ Object
static helpers.
- #normalize_region(x, y, width, height) ⇒ Object
- #offset_to_center(x, y, w, h) ⇒ Object
- #thumbnailing_method(name, &block) ⇒ Object
- #ufloat!(name, arg, default = nil) ⇒ Object
- #uint!(name, arg, default = nil) ⇒ Object
- #with_default(arg, default = nil) ⇒ Object
Constructor Details
#initialize(&block) ⇒ PluginContext
Returns a new instance of PluginContext.
9 10 11 12 13 |
# File 'lib/httpthumbnailer/plugin.rb', line 9 def initialize(&block) @thumbnailing_methods = [] @edits = [] instance_eval(&block) end |
Instance Attribute Details
#edits ⇒ Object (readonly)
Returns the value of attribute edits.
7 8 9 |
# File 'lib/httpthumbnailer/plugin.rb', line 7 def edits @edits end |
#thumbnailing_methods ⇒ Object (readonly)
Returns the value of attribute thumbnailing_methods.
6 7 8 |
# File 'lib/httpthumbnailer/plugin.rb', line 6 def thumbnailing_methods @thumbnailing_methods end |
Class Method Details
.from_file(file) ⇒ Object
15 16 17 18 19 |
# File 'lib/httpthumbnailer/plugin.rb', line 15 def self.from_file(file) self.new do instance_eval file.read, file.to_s end end |
Instance Method Details
#center_to_offset(center_x, center_y, w, h) ⇒ Object
73 74 75 |
# File 'lib/httpthumbnailer/plugin.rb', line 73 def center_to_offset(center_x, center_y, w, h) [center_x - w / 2, center_y - h / 2] end |
#edit(name, &block) ⇒ Object
27 28 29 30 31 |
# File 'lib/httpthumbnailer/plugin.rb', line 27 def edit(name, &block) name.kind_of? String or fail "edit name must ba a string; got: #{name.class.name}" block.kind_of? Proc or fail "edit '#{name}' needs to provide an implementation; got: #{name.class.name}" @edits << [name, block] end |
#float!(name, arg, default = nil) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/httpthumbnailer/plugin.rb', line 54 def float!(name, arg, default = nil) value = with_default(arg, default) or raise PluginArgumentError, "expected argument '#{name}' to be a float but got no value" begin Float(value) rescue ArgumentError raise PluginArgumentError, "expected argument '#{name}' to be a float, got: #{arg}" end end |
#int!(name, arg, default = nil) ⇒ Object
static helpers
39 40 41 42 43 44 45 46 |
# File 'lib/httpthumbnailer/plugin.rb', line 39 def int!(name, arg, default = nil) value = with_default(arg, default) or raise PluginArgumentError, "expected argument '#{name}' to be an integer but got no value" begin Integer(value) rescue ArgumentError raise PluginArgumentError, "expected argument '#{name}' to be an integer, got: #{arg.inspect}" end end |
#normalize_region(x, y, width, height) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/httpthumbnailer/plugin.rb', line 77 def normalize_region(x, y, width, height) x = 0.0 if x < 0 y = 0.0 if y < 0 width = 1.0 - x if width + x > 1 height = 1.0 - y if height + y > 1 width = Float::EPSILON if width < 0 height = Float::EPSILON if height < 0 [x, y, width, height] end |
#offset_to_center(x, y, w, h) ⇒ Object
69 70 71 |
# File 'lib/httpthumbnailer/plugin.rb', line 69 def offset_to_center(x, y, w, h) [x + w / 2, y + h / 2] end |
#thumbnailing_method(name, &block) ⇒ Object
21 22 23 24 25 |
# File 'lib/httpthumbnailer/plugin.rb', line 21 def thumbnailing_method(name, &block) name.kind_of? String or fail "thumbnailing method name must ba a string; got: #{name.class.name}" block.kind_of? Proc or fail "thumbnailing method '#{name}' needs to provide an implementation; got: #{name.class.name}" @thumbnailing_methods << [name, block] end |
#ufloat!(name, arg, default = nil) ⇒ Object
63 64 65 66 67 |
# File 'lib/httpthumbnailer/plugin.rb', line 63 def ufloat!(name, arg, default = nil) ret = float!(name, arg, default) ret < 0 and raise PluginArgumentError, "expected argument '#{name}' to be an unsigned float, got negative value: #{arg}" ret end |
#uint!(name, arg, default = nil) ⇒ Object
48 49 50 51 52 |
# File 'lib/httpthumbnailer/plugin.rb', line 48 def uint!(name, arg, default = nil) ret = int!(name, arg, default) ret < 0 and raise PluginArgumentError, "expected argument '#{name}' to be an unsigned integer, got negative value: #{arg}" ret end |
#with_default(arg, default = nil) ⇒ Object
33 34 35 36 |
# File 'lib/httpthumbnailer/plugin.rb', line 33 def with_default(arg, default = nil) return default if arg.nil? or arg == '' arg end |